xdp: Add tracepoint on XDP program return
Subsystems (6)
RFC PATCH 1/1 xdp: Add tracepoint on XDP program return
This adds a new tracepoint, xdp_prog_return, which is triggered at every XDP program return. This was first discussed back in August[0] as a way to hook XDP into the kernel drop_monitor framework, to have a one-stop place to find all packet drops in the system.
Because trace/events/xdp.h includes filter.h, some ifdef guarding is needed to be able to use the tracepoint from bpf_prog_run_xdp(). If anyone has any ideas for how to improve on this, please to speak up. Sending this RFC because of this issue, and to get some feedback from Ido on whether this tracepoint has enough data for drop_monitor usage.
[0] https://lore.kernel.org/netdev/20190809125418.GB2931@splinter/
--- include/linux/filter.h | 22 +++++++++++++++++-- include/trace/events/xdp.h | 45 ++++++++++++++++++++++++++++++++++++++ kernel/bpf/core.c | 2 ++ 3 files changed, 67 insertions(+), 2 deletions(-)
include/linux/filter.h +20 -2
@@ -704,19 +704,37 @@ static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog, DECLARE_BPF_DISPATCHER(bpf_dispatcher_xdp) +#if defined(_XDP_TRACE_DEF) || defined(_TRACE_XDP_H) +static void call_trace_xdp_prog_return (const struct xdp_buff *xdp, +const struct bpf_prog *prog, +u32 act); +#else +#ifndef _CALL_TRACE_XDP +#define _CALL_TRACE_XDP +static inline void call_trace_xdp_prog_return (const struct xdp_buff *xdp, +const struct bpf_prog *prog, +u32 act) {} +#endif +#endif + static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,struct xdp_buff *xdp) { +u32 ret; +/* Caller needs to hold rcu_read_lock() (!), otherwise program * can be released while still running, or map elements could be * freed early while still having concurrent users. XDP fastpath * already takes rcu_read_lock() when fetching the program, so * it's not necessary here anymore. */ -return __BPF_PROG_RUN (prog, xdp, -BPF_DISPATCHER_FUNC (bpf_dispatcher_xdp)); + ret =__BPF_PROG_RUN (prog, xdp, +BPF_DISPATCHER_FUNC (bpf_dispatcher_xdp)); +call_trace_xdp_prog_return (xdp, prog, ret); +return ret; } +void bpf_prog_change_xdp (struct bpf_prog *prev_prog,struct bpf_prog *prog);static inline u32 bpf_prog_insn_size (const struct bpf_prog *prog)
include/trace/events/xdp.h +45 -0
51 lines not on this page · raw message
kernel/bpf/core.c +2 -0
@@ -17,6 +17,8 @@* Kris Katterjohn - Added many additional checks in bpf_check_classic() */ +#define _XDP_TRACE_DEF +#include <uapi/linux/btf.h> #include <linux/filter.h> #include <linux/skbuff.h>
Signature · 1 line
2.24.1
Quoted 6 lines · Toke Høiland-Jørgensen
This adds a new tracepoint,
xdp_prog_return, which is triggered at every XDP program return. This was first discussed back in August[0] as a way to hook XDP into the kerneldrop_monitorframework, to have a one-stop place to find all packet drops in the system.
Quoted 6 lines
Because
trace/events/xdp.hincludesfilter.h, some ifdef guarding is needed to be able to use the tracepoint frombpf_prog_run_xdp(). If anyone has any ideas for how to improve on this, please to speak up. Sending this RFC because of this issue, and to get some feedback from Ido on whether this tracepoint has enough data fordrop_monitorusage.
I get that it would be useful, but can it be solved with BPF tracing (i.e. tracing BPF with BPF)? It would be neat not adding another tracepoint in the fast-path...
Quoted 14 lines · Björn Töpel
Quoted 6 lines · Toke Høiland-Jørgensen
This adds a new tracepoint,
xdp_prog_return, which is triggered at every XDP program return. This was first discussed back in August[0] as a way to hook XDP into the kerneldrop_monitorframework, to have a one-stop place to find all packet drops in the system.Quoted 6 lines
Because
trace/events/xdp.hincludesfilter.h, some ifdef guarding is needed to be able to use the tracepoint frombpf_prog_run_xdp(). If anyone has any ideas for how to improve on this, please to speak up. Sending this RFC because of this issue, and to get some feedback from Ido on whether this tracepoint has enough data fordrop_monitorusage.
Quoted 3 lines
I get that it would be useful, but can it be solved with BPF tracing (i.e. tracing BPF with BPF)? It would be neat not adding another tracepoint in the fast-path...
That was my question as well.
Here is an example from Eelco:
https://lore.kernel.org/bpf/[email protected]/
BPF_TRACE_2("fexit/xdp_prog_simple", trace_on_exit,
struct xdp_buff*, xdp, int, ret)
{
bpf_debug("fexit: [ifindex = %u, queue = %u, ret = %d]\n",
xdp->rxq->dev->ifindex, xdp->rxq->queue_index, ret);
return 0;
}'ret' is return code from xdp program.
Such approach is per xdp program, but cheaper when not enabled and faster when it's triggering comparing to static tracepoint. Anything missing there that you'd like to see?
Quoted 30 lines · Alexei Starovoitov
Quoted 14 lines · Björn Töpel
Quoted 6 lines · Toke Høiland-Jørgensen
This adds a new tracepoint,
xdp_prog_return, which is triggered at every XDP program return. This was first discussed back in August[0] as a way to hook XDP into the kerneldrop_monitorframework, to have a one-stop place to find all packet drops in the system.Quoted 6 lines
Because
trace/events/xdp.hincludesfilter.h, some ifdef guarding is needed to be able to use the tracepoint frombpf_prog_run_xdp(). If anyone has any ideas for how to improve on this, please to speak up. Sending this RFC because of this issue, and to get some feedback from Ido on whether this tracepoint has enough data fordrop_monitorusage.Quoted 3 lines
I get that it would be useful, but can it be solved with BPF tracing (i.e. tracing BPF with BPF)? It would be neat not adding another tracepoint in the fast-path...
That was my question as well.
Here is an example from Eelco:
https://lore.kernel.org/bpf/[email protected]/
BPF_TRACE_2("fexit/xdp_prog_simple",trace_on_exit,
struct xdp_buff*, xdp, int, ret){ bpf_debug("fexit: [ifindex = %u, queue = %u, ret = %d]\n", xdp->rxq->dev->ifindex, xdp->rxq->queue_index, ret); return 0; }
Quoted 4 lines
'ret' is return code from xdp program.
Such approach is per xdp program, but cheaper when not enabled and faster when it's triggering comparing to static tracepoint. Anything missing there that you'd like to see?
For userspace, sure, the fentry/fexit stuff is fine. The main use case for this new tracepoint is to hook into the (in-kernel) drop monitor. Dunno if that can be convinced to hook into the BPF tracing infrastructure instead of tracepoints. Ido, WDYT?
-Toke
Quoted 37 lines · Toke Høiland-Jørgensen
Quoted 30 lines · Alexei Starovoitov
Quoted 14 lines · Björn Töpel
Quoted 6 lines · Toke Høiland-Jørgensen
This adds a new tracepoint,
xdp_prog_return, which is triggered at every XDP program return. This was first discussed back in August[0] as a way to hook XDP into the kerneldrop_monitorframework, to have a one-stop place to find all packet drops in the system.Quoted 6 lines
Because
trace/events/xdp.hincludesfilter.h, some ifdef guarding is needed to be able to use the tracepoint frombpf_prog_run_xdp(). If anyone has any ideas for how to improve on this, please to speak up. Sending this RFC because of this issue, and to get some feedback from Ido on whether this tracepoint has enough data fordrop_monitorusage.Quoted 3 lines
I get that it would be useful, but can it be solved with BPF tracing (i.e. tracing BPF with BPF)? It would be neat not adding another tracepoint in the fast-path...
That was my question as well.
Here is an example from Eelco:
https://lore.kernel.org/bpf/[email protected]/
BPF_TRACE_2("fexit/xdp_prog_simple",trace_on_exit,
struct xdp_buff*, xdp, int, ret){ bpf_debug("fexit: [ifindex = %u, queue = %u, ret = %d]\n", xdp->rxq->dev->ifindex, xdp->rxq->queue_index, ret); return 0; }Quoted 4 lines
'ret' is return code from xdp program.
Such approach is per xdp program, but cheaper when not enabled and faster when it's triggering comparing to static tracepoint. Anything missing there that you'd like to see?
Quoted 4 lines
For userspace, sure, the fentry/fexit stuff is fine. The main use case for this new tracepoint is to hook into the (in-kernel) drop monitor. Dunno if that can be convinced to hook into the BPF tracing infrastructure instead of tracepoints. Ido, WDYT?
Hi Toke,
Sorry for the delay. I wasn't available most of last week.
Regarding the tracepoint, the data it provides seems sufficient to me. Regarding the fentry/fexit stuff, it would be great to hook it into drop monitor, but I'm not sure how to do that at this point. It seems that at minimum user would need to pass the XDP programs that need to be traced?
FYI, I'm not too happy with the current way of capturing the events via nlmon, so I started creating a utility to directly output the events to pcap [1] (inspired by Florian's nfqdump). Will send a pull request to Neil when it's ready. You can do:
# dwdump -w /dev/stdout | tshark -V -r -
A recent enough wireshark will correctly dissect these events. My next step is to add '--unique' which will load an eBPF program on the socket and only allow unique events to be enqueued. The program will store {5-tuple, IP/drop reason} in LRU hash with corresponding count. I can then instrument the application for Prometheus so that it will export the contents of the map as metrics.
Please let me know if you have more suggestions.
[1] https://github.com/idosch/dropwatch/blob/dwdump/src/dwdump.c