samples: bpf: Refactor XDP programs with libbpf
Subsystems (4)
v2 cover samples: bpf: Refactor XDP programs with libbpf
To avoid confusion caused by the increasing fragmentation of the BPF Loader program, this commit would like to convert the previous bpf_load loader with the libbpf loader.
Thanks to libbpf's bpf_link interface, managing the tracepoint BPF program is much easier. bpf_program__attach_tracepoint manages the enable of tracepoint event and attach of BPF programs to it with a single interface bpf_link, so there is no need to manage event_fd and prog_fd separately.
And due to addition of generic bpf_program__attach() to libbpf, it is now possible to attach BPF programs with __attach() instead of explicitly calling __attach_<type>().
This patchset refactors xdp_monitor with using this libbpf API, and the bpf_load is removed and migrated to libbpf. Also, attach_tracepoint() is replaced with the generic __attach() method in xdp_redirect_cpu. Moreover, maps in kern program have been converted to BTF-defined map.
---
Changes in v2:
- added cleanup logic for bpf_link and bpf_object in xdp_monitor
- program section match with bpf_program__is_<type> instead of strncmp
- revert BTF key/val type to default of BPF_MAP_TYPE_PERF_EVENT_ARRAY
- split increment into seperate satement
- refactor pointer array initialization
- error code cleanup
Daniel T. Lee (3):
samples: bpf: Refactor xdp_monitor with libbpf
samples: bpf: Replace attach_tracepoint() to attach() in
xdp_redirect_cpu
samples: bpf: refactor XDP kern program maps with BTF-defined map
samples/bpf/Makefile | 4 +- samples/bpf/xdp_monitor_kern.c | 60 +++++------ samples/bpf/xdp_monitor_user.c | 159 +++++++++++++++++++++------- samples/bpf/xdp_redirect_cpu_user.c | 153 +++++++++++++------------- samples/bpf/xdp_sample_pkts_kern.c | 14 ++- samples/bpf/xdp_sample_pkts_user.c | 1 - 6 files changed, 230 insertions(+), 161 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/3 samples: bpf: Refactor xdp_monitor with libbpf
To avoid confusion caused by the increasing fragmentation of the BPF Loader program, this commit would like to change to the libbpf loader instead of using the bpf_load.
Thanks to libbpf's bpf_link interface, managing the tracepoint BPF program is much easier. bpf_program__attach_tracepoint manages the enable of tracepoint event and attach of BPF programs to it with a single interface bpf_link, so there is no need to manage event_fd and prog_fd separately.
This commit refactors xdp_monitor with using this libbpf API, and the bpf_load is removed and migrated to libbpf.
---
Changes in v2:
- added cleanup logic for bpf_link and bpf_object
- split increment into seperate satement
- refactor pointer array initialization
samples/bpf/Makefile | 2 +- samples/bpf/xdp_monitor_user.c | 159 +++++++++++++++++++++++++-------- 2 files changed, 121 insertions(+), 40 deletions(-)
samples/bpf/Makefile +1 -1
@@ -99,7 +99,7 @@ per_socket_stats_example-objs := cookie_uid_helper_example.o xdp_redirect-objs := xdp_redirect_user.o xdp_redirect_map-objs := xdp_redirect_map_user.o xdp_redirect_cpu-objs := bpf_load.o xdp_redirect_cpu_user.o -xdp_monitor-objs := bpf_load.o xdp_monitor_user.o +xdp_monitor-objs := xdp_monitor_user.o xdp_rxq_info-objs := xdp_rxq_info_user.o syscall_tp-objs := syscall_tp_user.o cpustat-objs := cpustat_user.o
samples/bpf/xdp_monitor_user.c +120 -39
253 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH bpf-next v2 1/3] samples: bpf: Refactor xdp_monitor with libbpf
Quoted 11 lines · Daniel T. Lee
To avoid confusion caused by the increasing fragmentation of the BPF Loader program, this commit would like to change to the libbpf loader instead of using the
bpf_load.Thanks to libbpf's
bpf_linkinterface, managing the tracepoint BPF program is much easier.bpf_program__attach_tracepointmanages the enable of tracepoint event and attach of BPF programs to it with a single interfacebpf_link, so there is no need to manageevent_fdandprog_fdseparately.
Quoted 6 lines
This commit refactors
xdp_monitorwith using this libbpf API, and thebpf_loadis removed and migrated to libbpf.Signed-off-by Daniel T. Lee <[email protected]>---
Quoted 9 lines
Changes in v2:
- added cleanup logic forbpf_linkandbpf_object
- split increment into seperate satement
- refactor pointer array initializationsamples/bpf/Makefile | 2 +- samples/bpf/xdp_monitor_user.c | 159 +++++++++++++++++++++++++-------- 2 files changed, 121 insertions(+), 40 deletions(-)
[...]
PATCH v2 2/3 samples: bpf: Replace attach_tracepoint() to attach() in xdp_redirect_cpu
In reply to Daniel T. LeeFrom commit d7a18ea7e8b6 ("libbpf: Add generic bpf_program__attach()"), for some BPF programs, it is now possible to attach BPF programs with __attach() instead of explicitly calling __attach_<type>().
This commit refactors the __attach_tracepoint() with libbpf's generic __attach() method. In addition, this refactors the logic of setting the map FD to simplify the code. Also, the missing removal of bpf_load.o in Makefile has been fixed.
---
Changes in v2:
- program section match with bpf_program__is_<type> instead of strncmp
- refactor pointer array initialization
- error code cleanup
samples/bpf/Makefile | 2 +- samples/bpf/xdp_redirect_cpu_user.c | 153 +++++++++++++--------------- 2 files changed, 73 insertions(+), 82 deletions(-)
samples/bpf/Makefile +1 -1
@@ -98,7 +98,7 @@ test_map_in_map-objs := test_map_in_map_user.o per_socket_stats_example-objs := cookie_uid_helper_example.o xdp_redirect-objs := xdp_redirect_user.o xdp_redirect_map-objs := xdp_redirect_map_user.o -xdp_redirect_cpu-objs := bpf_load.o xdp_redirect_cpu_user.o +xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o xdp_monitor-objs := xdp_monitor_user.o xdp_rxq_info-objs := xdp_rxq_info_user.o syscall_tp-objs := syscall_tp_user.o
samples/bpf/xdp_redirect_cpu_user.c +72 -81
270 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH bpf-next v2 2/3] samples: bpf: Replace attach_tracepoint() to attach() in xdp_redirect_cpu
Quoted 22 lines · Daniel T. Lee
From commit
d7a18ea7e8b6("libbpf: Add genericbpf_program__attach()"), for some BPF programs, it is now possible to attach BPF programs with__attach()instead of explicitly calling __attach_<type>().This commit refactors the
__attach_tracepoint()with libbpf's generic__attach()method. In addition, this refactors the logic of setting the map FD to simplify the code. Also, the missing removal of bpf_load.o inMakefilehas been fixed.Signed-off-by Daniel T. Lee <[email protected]>---
Changes in v2:
- program section match with bpf_program__is_<type> instead of strncmp
- refactor pointer array initialization
- error code cleanupsamples/bpf/Makefile | 2 +- samples/bpf/xdp_redirect_cpu_user.c | 153 +++++++++++++--------------- 2 files changed, 73 insertions(+), 82 deletions(-)
[...]
samples/bpf/xdp_redirect_cpu_user.c int main(int argc, char **argv) · line 980
Quoted 7 lines
samples/bpf/xdp_redirect_cpu_user.c +2
-0@@ -986,6 +975,8 @@ int main(int argc, char **argv)stats_poll (interval, use_separators, prog_name, mprog_name, &value, stress_mode); + + err =EXIT_OK ; out:
This one doesn't call bpf_object__close() as well, but let's land your refactoring and we can do further improvements in the follow ups.
Quoted 5 lines
samples/bpf/xdp_redirect_cpu_user.c +0
-1free (cpu);return err; --2.25.1
PATCH v2 3/3 samples: bpf: refactor XDP kern program maps with BTF-defined map
In reply to Daniel T. LeeMost of the samples were converted to use the new BTF-defined MAP as they moved to libbpf, but some of the samples were missing.
Instead of using the previous BPF MAP definition, this commit refactors xdp_monitor and xdp_sample_pkts_kern MAP definition with the new BTF-defined MAP format.
Also, this commit removes the max_entries attribute at PERF_EVENT_ARRAY map type. The libbpf's bpf_object__create_map() will automatically set max_entries to the maximum configured number of CPUs on the host.
---
Changes in v2:
- revert BTF key/val type to default of BPF_MAP_TYPE_PERF_EVENT_ARRAY
samples/bpf/xdp_monitor_kern.c | 60 +++++++++++++++--------------- samples/bpf/xdp_sample_pkts_kern.c | 14 +++---- samples/bpf/xdp_sample_pkts_user.c | 1 - 3 files changed, 36 insertions(+), 39 deletions(-)
samples/bpf/xdp_monitor_kern.c +30 -30
@@ -6,21 +6,21 @@#include <uapi/linux/bpf.h> #include <bpf/bpf_helpers.h> -struct bpf_map_def SEC("maps") redirect_err_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(u64), - .max_entries = 2, +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __type(key, u32); + __type(value, u64); + __uint(max_entries, 2);/* TODO: have entries for all possible errno's */ -}; +} redirect_err_cnt SEC(".maps");#define XDP_UNKNOWN XDP_REDIRECT + 1 -struct bpf_map_def SEC("maps") exception_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(u64), - .max_entries = XDP_UNKNOWN + 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __type(key, u32); + __type(value, u64); + __uint(max_entries, XDP_UNKNOWN + 1); +} exception_cnt SEC(".maps");/* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_redirect/format * Code in: kernel/include/trace/events/xdp.h
@@ -129,19 +129,19 @@ struct datarec { };#define MAX_CPUS 64 -struct bpf_map_def SEC("maps") cpumap_enqueue_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = MAX_CPUS, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __type(key, u32); + __type(value, struct datarec); + __uint(max_entries, MAX_CPUS); +} cpumap_enqueue_cnt SEC(".maps"); -struct bpf_map_def SEC("maps") cpumap_kthread_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __type(key, u32); + __type(value, struct datarec); + __uint(max_entries, 1); +} cpumap_kthread_cnt SEC(".maps");/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_enqueue/format * Code in: kernel/include/trace/events/xdp.h
@@ -210,12 +210,12 @@ int trace_xdp_cpumap_kthread(struct cpumap_kthread_ctx *ctx)return 0 ; } -struct bpf_map_def SEC("maps") devmap_xmit_cnt = { - .type = BPF_MAP_TYPE_PERCPU_ARRAY, - .key_size = sizeof(u32), - .value_size = sizeof(struct datarec), - .max_entries = 1, -}; +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __type(key, u32); + __type(value, struct datarec); + __uint(max_entries, 1); +} devmap_xmit_cnt SEC(".maps");/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_devmap_xmit/format * Code in: kernel/include/trace/events/xdp.h
samples/bpf/xdp_sample_pkts_kern.c +6 -8
@@ -5,14 +5,12 @@#include <bpf/bpf_helpers.h> #define SAMPLE_SIZE 64ul -#define MAX_CPUS 128 - -struct bpf_map_def SEC("maps") my_map = { - .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, - .key_size = sizeof(int), - .value_size = sizeof(u32), - .max_entries = MAX_CPUS, -}; + +struct { + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(u32)); +} my_map SEC(".maps"); SEC("xdp_sample")int xdp_sample_prog (struct xdp_md *ctx)
samples/bpf/xdp_sample_pkts_user.c +0 -1
@@ -18,7 +18,6 @@#include "perf-sys.h" -#define MAX_CPUS 128static int if_idx;static char *if_name;static __u32 xdp_flags =XDP_FLAGS_UPDATE_IF_NOEXIST ;
Signature · 1 line
2.25.1
Re: [PATCH bpf-next v2 3/3] samples: bpf: refactor XDP kern program maps with BTF-defined map
Quoted 12 lines · Daniel T. Lee
Most of the samples were converted to use the new BTF-defined MAP as they moved to libbpf, but some of the samples were missing.
Instead of using the previous BPF MAP definition, this commit refactors
xdp_monitorandxdp_sample_pkts_kernMAP definition with the new BTF-defined MAP format.Also, this commit removes the
max_entriesattribute atPERF_EVENT_ARRAYmap type. The libbpf'sbpf_object__create_map()will automatically setmax_entriesto the maximum configured number of CPUs on the host.
Quoted 6 lines
Signed-off-by Daniel T. Lee <[email protected]>---
Changes in v2:
- revert BTF key/val type to default ofBPF_MAP_TYPE_PERF_EVENT_ARRAY
Quoted 5 lines
samples/bpf/xdp_monitor_kern.c | 60 +++++++++++++++--------------- samples/bpf/xdp_sample_pkts_kern.c | 14 +++---- samples/bpf/xdp_sample_pkts_user.c | 1 - 3 files changed, 36 insertions(+), 39 deletions(-)
[...]