samples: bpf: Refactor XDP programs with libbpf
Subsystems (4)
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.
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 | 144 +++++++++++++++++++++------- samples/bpf/xdp_redirect_cpu_user.c | 138 +++++++++++++------------- samples/bpf/xdp_sample_pkts_kern.c | 14 ++- samples/bpf/xdp_sample_pkts_user.c | 1 - 6 files changed, 211 insertions(+), 150 deletions(-)
Signature · 1 line
2.25.1
PATCH 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.
--- samples/bpf/Makefile | 2 +- samples/bpf/xdp_monitor_user.c | 144 ++++++++++++++++++++++++--------- 2 files changed, 108 insertions(+), 38 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 +107 -37
235 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH bpf-next 1/3] samples: bpf: Refactor xdp_monitor with libbpf
Quoted 20 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.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]>--- samples/bpf/Makefile | 2 +- samples/bpf/xdp_monitor_user.c | 144 ++++++++++++++++++++++++--------- 2 files changed, 108 insertions(+), 38 deletions(-)
[...]
samples/bpf/xdp_monitor_user.c static const char *__doc_err_only__= · line 58
Quoted 6 lines
samples/bpf/xdp_monitor_user.c +4
-0+static int tp_cnt; +static int map_cnt;static int verbose =1 ;static bool debug = false; +struct bpf_map *map_data[NUM_MAP ] = {0 }; +struct bpf_link *tp_links[NUM_TP ] = {0 };
this syntax means "initialize *only the first element* to 0 (explicitly) and the rest of elements to default (which is also 0)". So it's just misleading, use = {}.
samples/bpf/xdp_monitor_user.c static const struct option long_options[] = { · line 73
Quoted 13 lines
static const struct option long_options[] = {
{"help",no_argument, NULL, 'h' },samples/bpf/xdp_monitor_user.c +6
-0@@ -41,6 +65,15 @@ static const struct option long_options[] = { {0, 0, NULL, 0 } }; +static void int_exit (int sig) +{ +/* Detach tracepoints */ +while (tp_cnt) +bpf_link__destroy (tp_links[--tp_cnt]); +
see below about proper cleanup
Quoted 6 lines
samples/bpf/xdp_monitor_user.c +3
-0+exit (0 ); +} +/* C standard specifies two constants, EXIT_SUCCESS(0) and EXIT_FAILURE(1) */ #define EXIT_FAIL_MEM 5
[...]
Quoted 21 lines
samples/bpf/xdp_monitor_user.c +8
-6-static void print_bpf_prog_info (void ) +static void print_bpf_prog_info (struct bpf_object *obj) { -int i; +struct bpf_program *prog; +struct bpf_map *map; +int i =0 ;/* Prog info */ -printf ("Loaded BPF prog have %d bpf program(s)\n" , prog_cnt); -for (i =0 ; i < prog_cnt; i++) { -printf (" - prog_fd[%d] = fd(%d)\n" , i, prog_fd[i]); +printf ("Loaded BPF prog have %d bpf program(s)\n" , tp_cnt); +bpf_object__for_each_program (prog, obj) { +printf (" - prog_fd[%d] = fd(%d)\n" , i++,bpf_program__fd (prog)); } + i =0 ;/* Maps info */ -printf ("Loaded BPF prog have %d map(s)\n" , map_data_count);
samples/bpf/xdp_monitor_user.c static void stats_poll(int interval, bool err_only) · line 671
Quoted 10 lines
samples/bpf/xdp_monitor_user.c +5
-4-for (i =0 ; i < map_data_count; i++) { -char *name = map_data[i].name; -int fd = map_data[i].fd; +printf ("Loaded BPF prog have %d map(s)\n" , map_cnt); +bpf_object__for_each_map (map, obj) { +const char *name =bpf_map__name (map); +int fd =bpf_map__fd (map); -printf (" - map_data[%d] = fd(%d) name:%s\n" , i, fd, name); +printf (" - map_data[%d] = fd(%d) name:%s\n" , i++, fd, name);
please move out increment into a separate statement, no need to confuse readers unnecessarily
Quoted 18 lines
} /* Event info */samples/bpf/xdp_monitor_user.c +6
-4-printf ("Searching for (max:%d) event file descriptor(s)\n" , prog_cnt); -for (i =0 ; i < prog_cnt; i++) { -if (event_fd[i] !=-1 ) -printf (" - event_fd[%d] = fd(%d)\n" , i, event_fd[i]); +printf ("Searching for (max:%d) event file descriptor(s)\n" , tp_cnt); +for (i =0 ; i < tp_cnt; i++) { +int fd =bpf_link__fd (tp_links[i]); + +if (fd !=-1 ) +printf (" - event_fd[%d] = fd(%d)\n" , i, fd); } }int main (int argc,char **argv) {
[...]
samples/bpf/xdp_monitor_user.c int main(int argc, char **argv) · line 736
Quoted 11 lines
samples/bpf/xdp_monitor_user.c +7
-2+ obj =bpf_object__open_file (filename,NULL ); +if (libbpf_get_error (obj)) { +printf ("ERROR: opening BPF object file failed\n" ); + obj =NULL ;return EXIT_FAILURE ; } -if (!prog_fd[0 ]) { -printf ("ERROR - load_bpf_file: %s\n" ,strerror (errno)); + +/* load BPF program */ +if (bpf_object__load (obj)) {
would be still good to call bpf_object__close(obj) here, this will avoid warnings about memory leaks, if you run this program under ASAN
samples/bpf/xdp_monitor_user.c int main(int argc, char **argv) · line 746
Quoted 10 lines
samples/bpf/xdp_monitor_user.c +7
-0+printf ("ERROR: loading BPF object file failed\n" );return EXIT_FAILURE ; } +for (type =0 ; type <NUM_MAP ; type++) { + map_data[type] = +bpf_object__find_map_by_name (obj, map_type_strings[type]); + +if (libbpf_get_error (map_data[type])) { +printf ("ERROR: finding a map in obj file failed\n" );
same about cleanup, goto into single cleanup place would be appropriate throughout this entire function, probably.
Quoted 5 lines
samples/bpf/xdp_monitor_user.c +5
-0+return EXIT_FAILURE ; + } + map_cnt++; + } +
[...]
Re: [PATCH bpf-next 1/3] samples: bpf: Refactor xdp_monitor with libbpf
Quoted 25 lines · Andrii Nakryiko
Quoted 20 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.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]>--- samples/bpf/Makefile | 2 +- samples/bpf/xdp_monitor_user.c | 144 ++++++++++++++++++++++++--------- 2 files changed, 108 insertions(+), 38 deletions(-)[...]
Quoted 11 lines
samples/bpf/xdp_monitor_user.c static const char *__doc_err_only__= · line 58
Quoted 6 lines
samples/bpf/xdp_monitor_user.c +4
-0+static int tp_cnt; +static int map_cnt;static int verbose =1 ;static bool debug = false; +struct bpf_map *map_data[NUM_MAP ] = {0 }; +struct bpf_link *tp_links[NUM_TP ] = {0 };this syntax means "initialize *only the first element* to 0 (explicitly) and the rest of elements to default (which is also 0)". So it's just misleading, use
= {}.
Thanks for the great review!
Come to think of it, it could be confusing as you mentioned. I will remove the unnecessary initializer in the next patch and resend it.
Quoted 54 lines
samples/bpf/xdp_monitor_user.c static const struct option long_options[] = { · line 73
Quoted 13 lines
static const struct option long_options[] = {
{"help",no_argument, NULL, 'h' },samples/bpf/xdp_monitor_user.c +6
-0@@ -41,6 +65,15 @@ static const struct option long_options[] = { {0, 0, NULL, 0 } }; +static void int_exit (int sig) +{ +/* Detach tracepoints */ +while (tp_cnt) +bpf_link__destroy (tp_links[--tp_cnt]); +see below about proper cleanup
Quoted 6 lines
samples/bpf/xdp_monitor_user.c +3
-0+exit (0 ); +} +/* C standard specifies two constants, EXIT_SUCCESS(0) and EXIT_FAILURE(1) */ #define EXIT_FAIL_MEM 5[...]
Quoted 29 lines
samples/bpf/xdp_monitor_user.c +12
-9-static void print_bpf_prog_info (void ) +static void print_bpf_prog_info (struct bpf_object *obj) { -int i; +struct bpf_program *prog; +struct bpf_map *map; +int i =0 ;/* Prog info */ -printf ("Loaded BPF prog have %d bpf program(s)\n" , prog_cnt); -for (i =0 ; i < prog_cnt; i++) { -printf (" - prog_fd[%d] = fd(%d)\n" , i, prog_fd[i]); +printf ("Loaded BPF prog have %d bpf program(s)\n" , tp_cnt); +bpf_object__for_each_program (prog, obj) { +printf (" - prog_fd[%d] = fd(%d)\n" , i++,bpf_program__fd (prog)); } + i =0 ;/* Maps info */ -printf ("Loaded BPF prog have %d map(s)\n" , map_data_count); -for (i =0 ; i < map_data_count; i++) { -char *name = map_data[i].name; -int fd = map_data[i].fd; +printf ("Loaded BPF prog have %d map(s)\n" , map_cnt); +bpf_object__for_each_map (map, obj) { +const char *name =bpf_map__name (map); +int fd =bpf_map__fd (map);
Quoted 6 lines
samples/bpf/xdp_monitor_user.c static void stats_poll(int interval, bool err_only) · line 671
Quoted 2 lines
samples/bpf/xdp_monitor_user.c +1
-1-printf (" - map_data[%d] = fd(%d) name:%s\n" , i, fd, name); +printf (" - map_data[%d] = fd(%d) name:%s\n" , i++, fd, name);please move out increment into a separate statement, no need to confuse readers unnecessarily
I will fix it at the following patch.
Quoted 47 lines
Quoted 18 lines
} /* Event info */samples/bpf/xdp_monitor_user.c +6
-4-printf ("Searching for (max:%d) event file descriptor(s)\n" , prog_cnt); -for (i =0 ; i < prog_cnt; i++) { -if (event_fd[i] !=-1 ) -printf (" - event_fd[%d] = fd(%d)\n" , i, event_fd[i]); +printf ("Searching for (max:%d) event file descriptor(s)\n" , tp_cnt); +for (i =0 ; i < tp_cnt; i++) { +int fd =bpf_link__fd (tp_links[i]); + +if (fd !=-1 ) +printf (" - event_fd[%d] = fd(%d)\n" , i, fd); } }int main (int argc,char **argv) {[...]
samples/bpf/xdp_monitor_user.c int main(int argc, char **argv) · line 736
Quoted 11 lines
samples/bpf/xdp_monitor_user.c +7
-2+ obj =bpf_object__open_file (filename,NULL ); +if (libbpf_get_error (obj)) { +printf ("ERROR: opening BPF object file failed\n" ); + obj =NULL ;return EXIT_FAILURE ; } -if (!prog_fd[0 ]) { -printf ("ERROR - load_bpf_file: %s\n" ,strerror (errno)); + +/* load BPF program */ +if (bpf_object__load (obj)) {would be still good to call
bpf_object__close(obj)here, this will avoid warnings about memory leaks, if you run this program under ASANsamples/bpf/xdp_monitor_user.c int main(int argc, char **argv) · line 746
Quoted 10 lines
samples/bpf/xdp_monitor_user.c +7
-0+printf ("ERROR: loading BPF object file failed\n" );return EXIT_FAILURE ; } +for (type =0 ; type <NUM_MAP ; type++) { + map_data[type] = +bpf_object__find_map_by_name (obj, map_type_strings[type]); + +if (libbpf_get_error (map_data[type])) { +printf ("ERROR: finding a map in obj file failed\n" );
Quoted 3 lines
same about cleanup, goto into single cleanup place would be appropriate throughout this entire function, probably.
Jump to single cleanup will be much more intuitive. I will update and send the next version of patch right away.
Thank you for your time and effort for the review.
Best,
Daniel
Quoted 7 lines
Quoted 5 lines
samples/bpf/xdp_monitor_user.c +5
-0+return EXIT_FAILURE ; + } + map_cnt++; + } +[...]
PATCH 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.
--- samples/bpf/Makefile | 2 +- samples/bpf/xdp_redirect_cpu_user.c | 138 +++++++++++++--------------- 2 files changed, 67 insertions(+), 73 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 +66 -72
214 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH bpf-next 2/3] samples: bpf: Replace attach_tracepoint() to attach() in xdp_redirect_cpu
Quoted 16 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]>--- samples/bpf/Makefile | 2 +- samples/bpf/xdp_redirect_cpu_user.c | 138 +++++++++++++--------------- 2 files changed, 67 insertions(+), 73 deletions(-)
[...]
Quoted 3 lines
#define
NUM_TP5
+#defineNUM_MAP9
structbpf_link*tp_links[NUM_TP] = { 0 };
= {}
Quoted 4 lines
+static int map_fds[NUM_MAP];
static inttp_cnt= 0;/* Exit return codes */
[...]
Quoted 10 lines
samples/bpf/xdp_redirect_cpu_user.c +2
-6-static struct bpf_link *attach_tp (struct bpf_object *obj, -const char *tp_category, -const char * tp_name) +static int init_tracepoints (struct bpf_object *obj) { +char *tp_section ="tracepoint/" ;struct bpf_program *prog; -struct bpf_link *link; -char sec_name[PATH_MAX ]; -int len;
samples/bpf/xdp_redirect_cpu_user.c static void stats_poll(int interval, bool use_separators, char *prog_name, · line 694
Quoted 10 lines
samples/bpf/xdp_redirect_cpu_user.c +5
-4+const char *section; - len =snprintf (sec_name,PATH_MAX ,"tracepoint/%s/%s" , - tp_category, tp_name); -if (len <0 ) -exit (EXIT_FAIL ); +bpf_object__for_each_program (prog, obj) { + section =bpf_program__section_name (prog); +if (strncmp (section, tp_section,strlen (tp_section)) !=0 ) +continue ;
that's a convoluted and error-prone way (you can also use "tp/bla/bla" for tracepoint programs, for example). Use bpf_program__is_tracepoint() check.
Quoted 13 lines
samples/bpf/xdp_redirect_cpu_user.c +6
-4- prog =bpf_object__find_program_by_title (obj, sec_name); -if (!prog) { -fprintf (stderr,"ERR: finding progsec: %s\n" , sec_name); -exit (EXIT_FAIL_BPF ); + tp_links[tp_cnt] =bpf_program__attach (prog); +if (libbpf_get_error (tp_links[tp_cnt])) { + tp_links[tp_cnt] =NULL ; +return -EINVAL ; + } + tp_cnt++; }
[...]
Re: [PATCH bpf-next 2/3] samples: bpf: Replace attach_tracepoint() to attach() in xdp_redirect_cpu
Quoted 46 lines · Andrii Nakryiko
Quoted 16 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]>--- samples/bpf/Makefile | 2 +- samples/bpf/xdp_redirect_cpu_user.c | 138 +++++++++++++--------------- 2 files changed, 67 insertions(+), 73 deletions(-)[...]
Quoted 3 lines
#define
NUM_TP5
+#defineNUM_MAP9
structbpf_link*tp_links[NUM_TP] = { 0 };= {}
Quoted 4 lines
+static int map_fds[NUM_MAP];
static inttp_cnt= 0;/* Exit return codes */
[...]
Quoted 12 lines
samples/bpf/xdp_redirect_cpu_user.c +3
-6-static struct bpf_link *attach_tp (struct bpf_object *obj, -const char *tp_category, -const char * tp_name) +static int init_tracepoints (struct bpf_object *obj) { +char *tp_section ="tracepoint/" ;struct bpf_program *prog; -struct bpf_link *link; -char sec_name[PATH_MAX ]; -int len; +const char *section;
Quoted 13 lines
samples/bpf/xdp_redirect_cpu_user.c static void stats_poll(int interval, bool use_separators, char *prog_name, · line 694
Quoted 8 lines
samples/bpf/xdp_redirect_cpu_user.c +4
-4- len =snprintf (sec_name,PATH_MAX ,"tracepoint/%s/%s" , - tp_category, tp_name); -if (len <0 ) -exit (EXIT_FAIL ); +bpf_object__for_each_program (prog, obj) { + section =bpf_program__section_name (prog); +if (strncmp (section, tp_section,strlen (tp_section)) !=0 ) +continue ;that's a convoluted and error-prone way (you can also use "tp/bla/bla" for tracepoint programs, for example). Use
bpf_program__is_tracepoint()check.
Thanks for the review!
I think that's a much better way. I will send the next patch with applying that method.
Quoted 15 lines
Quoted 13 lines
samples/bpf/xdp_redirect_cpu_user.c +6
-4- prog =bpf_object__find_program_by_title (obj, sec_name); -if (!prog) { -fprintf (stderr,"ERR: finding progsec: %s\n" , sec_name); -exit (EXIT_FAIL_BPF ); + tp_links[tp_cnt] =bpf_program__attach (prog); +if (libbpf_get_error (tp_links[tp_cnt])) { + tp_links[tp_cnt] =NULL ; +return -EINVAL ; + } + tp_cnt++; }[...]
PATCH 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.
--- 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); + __type(key, int); + __type(value, 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 3/3] samples: bpf: refactor XDP kern program maps with BTF-defined map
Quoted 19 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.Signed-off-by Daniel T. Lee <[email protected]>--- 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(-)
[...]
Quoted 9 lines
samples/bpf/xdp_sample_pkts_kern.c +0
-3@@ -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 = {
samples/bpf/xdp_sample_pkts_kern.c line 12
Quoted 10 lines
samples/bpf/xdp_sample_pkts_kern.c +5
-5- .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); + __type(key, int); + __type(value, u32);
this actually will generate unnecessary libbpf warnings, because PERF_EVENT_ARRAY doesn't support BTF types for key/value. So use __uint(key_size, sizeof(int)) and __uint(value_size, sizeof(u32)) instead.
Quoted 19 lines
samples/bpf/xdp_sample_pkts_kern.c +1
-0+} my_map SEC(".maps"); SEC("xdp_sample")int xdp_sample_prog (struct xdp_md *ctx)samples/bpf/xdp_sample_pkts_user.c +0
-2@@ -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 ; --2.25.1
Re: [PATCH bpf-next 3/3] samples: bpf: refactor XDP kern program maps with BTF-defined map
Quoted 41 lines · Andrii Nakryiko
Quoted 19 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.Signed-off-by Daniel T. Lee <[email protected]>--- 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(-)[...]
Quoted 17 lines
samples/bpf/xdp_sample_pkts_kern.c +3
-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);
Quoted 9 lines
samples/bpf/xdp_sample_pkts_kern.c line 12
Quoted 2 lines
samples/bpf/xdp_sample_pkts_kern.c +2
-0+__type (key, int); +__type (value, u32);this actually will generate unnecessary libbpf warnings, because
PERF_EVENT_ARRAYdoesn't support BTF types for key/value. So use__uint(key_size, sizeof(int))and__uint(value_size, sizeof(u32))instead.
Thanks for the great review!
I'll fix it right away and send the next version of patch.
Quoted 19 lines
Quoted 19 lines
samples/bpf/xdp_sample_pkts_kern.c +1
-0+} my_map SEC(".maps"); SEC("xdp_sample")int xdp_sample_prog (struct xdp_md *ctx)samples/bpf/xdp_sample_pkts_user.c +0
-2@@ -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 ; --2.25.1
Quoted 34 lines · Daniel T. Lee
To avoid confusion caused by the increasing fragmentation of the BPF Loader program, this commit would like to convert the previous
bpf_loadloader with the libbpf loader.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.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_monitorwith using this libbpf API, and thebpf_loadis removed and migrated to libbpf. Also,attach_tracepoint()is replaced with the generic__attach()method inxdp_redirect_cpu. Moreover, maps in kern program have been converted to BTF-defined map.Daniel T. Lee (3):
samples: bpf: Refactorxdp_monitorwith libbpf
samples: bpf: Replaceattach_tracepoint()toattach()in
xdp_redirect_cpu
samples: bpf: refactor XDP kern program maps with BTF-defined mapsamples/bpf/Makefile | 4 +- samples/bpf/xdp_monitor_kern.c | 60 ++++++------ samples/bpf/xdp_monitor_user.c | 144 +++++++++++++++++++++------- samples/bpf/xdp_redirect_cpu_user.c | 138 +++++++++++++------------- samples/bpf/xdp_sample_pkts_kern.c | 14 ++- samples/bpf/xdp_sample_pkts_user.c | 1 - 6 files changed, 211 insertions(+), 150 deletions(-)
Quoted 3 lines
--
2.25.1
Thanks for this clean up, Daniel! It's great! I left a few nits here and there in the appropriate patches.
There still seem to be a bunch of users of bpf_load.c, which would be nice to get rid of completely. But before you go do that, consider integrating BPF skeleton into samples/bpf Makefile. That way instead of all those look ups of maps/programs by name, you'd be writing a straightforward skel->maps.my_map and similar short and non-failing code. This should make the overall time spent on conversion much smaller (and more pleasant, IMO).
You've dealt with a lot of samples/bpf reworking, so it should be too hard for you to figure out the best way to do this, but check selftests/bpf's Makefile, if you need some ideas. Or just ask for help. Thanks!
Quoted 51 lines · Andrii Nakryiko
Quoted 34 lines · Daniel T. Lee
To avoid confusion caused by the increasing fragmentation of the BPF Loader program, this commit would like to convert the previous
bpf_loadloader with the libbpf loader.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.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_monitorwith using this libbpf API, and thebpf_loadis removed and migrated to libbpf. Also,attach_tracepoint()is replaced with the generic__attach()method inxdp_redirect_cpu. Moreover, maps in kern program have been converted to BTF-defined map.Daniel T. Lee (3):
samples: bpf: Refactorxdp_monitorwith libbpf
samples: bpf: Replaceattach_tracepoint()toattach()in
xdp_redirect_cpu
samples: bpf: refactor XDP kern program maps with BTF-defined mapsamples/bpf/Makefile | 4 +- samples/bpf/xdp_monitor_kern.c | 60 ++++++------ samples/bpf/xdp_monitor_user.c | 144 +++++++++++++++++++++------- samples/bpf/xdp_redirect_cpu_user.c | 138 +++++++++++++------------- samples/bpf/xdp_sample_pkts_kern.c | 14 ++- samples/bpf/xdp_sample_pkts_user.c | 1 - 6 files changed, 211 insertions(+), 150 deletions(-)Quoted 3 lines
--
2.25.1Thanks for this clean up, Daniel! It's great! I left a few nits here and there in the appropriate patches.
There still seem to be a bunch of users of
bpf_load.c, which would be nice to get rid of completely. But before you go do that, consider integrating BPF skeleton intosamples/bpfMakefile. That way instead of all those look ups of maps/programs by name, you'd be writing a straightforwardskel->maps.my_mapand similar short and non-failing code. This should make the overall time spent on conversion much smaller (and more pleasant, IMO).
Quoted 4 lines
You've dealt with a lot of
samples/bpfreworking, so it should be too hard for you to figure out the best way to do this, but check selftests/bpf'sMakefile, if you need some ideas. Or just ask for help. Thanks!
Thanks for the great feedback!
Thank you for letting me know about the BPF features that I can apply. Currently, I'm not familiar with the BPF skeleton yet, but I'll take a good look at the BPF skeleton to apply it in a more advanced form.
Thank you for your time and effort for the review.
Signature · 2 lines
Best, Daniel T. Lee