samples/bpf: Add xdp_stat sample program
Subsystems (4)
PATCH 1/1 samples/bpf: Add xdp_stat sample program
At Facebook we use tail calls to jump between our firewall filters and our L4LB. This is a program I wrote to estimate per program performance by swapping out the entries in the program array with interceptors that take measurements and then jump to the original entries.
I found the sample programs to be invaluable in understanding how to use the libbpf API (as well as the test env from the xdp-tutorial repo for testing), and want to return the favor. I am currently working on my next iteration that uses fentry/fexit to be less invasive, but I thought it was an interesting PoC of what you can do with program arrays.
--- samples/bpf/Makefile | 3 + samples/bpf/xdp_stat_common.h | 28 ++ samples/bpf/xdp_stat_kern.c | 169 ++++++++ samples/bpf/xdp_stat_user.c | 746 ++++++++++++++++++++++++++++++++++ 4 files changed, 946 insertions(+) create mode 100644 samples/bpf/xdp_stat_common.h create mode 100644 samples/bpf/xdp_stat_kern.c create mode 100644 samples/bpf/xdp_stat_user.c
samples/bpf/Makefile +3 -0
@@ -53,6 +53,7 @@ tprogs-y += task_fd_query tprogs-y += xdp_sample_pkts tprogs-y += ibumad tprogs-y += hbm +tprogs-y += xdp_stat # Libbpf dependencies LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
@@ -109,6 +110,7 @@ task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS) ibumad-objs := bpf_load.o ibumad_user.o $(TRACE_HELPERS) hbm-objs := bpf_load.o hbm.o $(CGROUP_HELPERS) +xdp_stat-objs := xdp_stat_user.o # Tell kbuild to always build the programs always := $(tprogs-y)
@@ -170,6 +172,7 @@ always += ibumad_kern.o always += hbm_out_kern.o always += hbm_edt_kern.o always += xdpsock_kern.o +always += xdp_stat_kern.o ifeq ($(ARCH), arm) # Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
samples/bpf/xdp_stat_common.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 +* Copyright (c) 2019 Facebook +*/ + +#ifndef __XDP_STAT_COMMON_HEADER_H +#define __XDP_STAT_COMMON_HEADER_H + +/* WARNING: Must match the number of interceptors generated in +* xdp_stat_kern.c including xdp/interceptor_0. +*/ +#define MAX_INTERCEPTORS 10 + +/* prog_stats_rec contains stats specific to an intercepted BPF program. +*/ +struct prog_stats_rec { +__u64 nr_terminal_runs; +__u64 nr_chained_runs; +__u64 ns_chained_runtime; +}; + +/* interception_info_rec contains global to all interceptors on a single CPU. +*/ +struct interception_info_rec { +__u32 prev_interceptor_nr; +__u64 prev_ns_start; +}; + +#endif /* __XDP_STAT_COMMON_HEADER_H */
samples/bpf/xdp_stat_kern.c new file +169 -0
169 lines not on this page · raw message
samples/bpf/xdp_stat_user.c new file +746 -0
746 lines not on this page · raw message
Signature · 1 line
2.24.1
Quoted 13 lines · Eric Sage
At Facebook we use tail calls to jump between our firewall filters and our L4LB. This is a program I wrote to estimate per program performance by swapping out the entries in the program array with interceptors that take measurements and then jump to the original entries.
I found the sample programs to be invaluable in understanding how to use the libbpf API (as well as the test env from the xdp-tutorial repo for testing), and want to return the favor. I am currently working on my next iteration that uses fentry/fexit to be less invasive, but I thought it was an interesting PoC of what you can do with program arrays.
Quoted 2 lines
Signed-off-by Eric Sage <[email protected]>---
Hey Eric,
Thanks for contributing this to samples. Ideally this would use BPF skeleton to simplify all the map and program look ups, but given samples/bpf doesn't have a infrastructure set up for this, it might be a bit too much effort at this point. So I think it's ok to do it this way, even though it would be really nice to try.
But please update the old-style bpf_map_def to BTF-defined maps before this gets merged (see below).
Quoted 9 lines
samples/bpf/Makefile | 3 + samples/bpf/xdp_stat_common.h | 28 ++ samples/bpf/xdp_stat_kern.c | 169 ++++++++ samples/bpf/xdp_stat_user.c | 746 ++++++++++++++++++++++++++++++++++ 4 files changed, 946 insertions(+) create mode 100644 samples/bpf/xdp_stat_common.h create mode 100644 samples/bpf/xdp_stat_kern.c create mode 100644 samples/bpf/xdp_stat_user.c
[...]
Quoted 7 lines
samples/bpf/xdp_stat_kern.c new file +7
-0+#include <uapi/linux/bpf.h> +#include "bpf_helpers.h" + +#include "xdp_stat_common.h" + +#define MAX_PROG_ARRAY 64 +
samples/bpf/xdp_stat_kern.c line 40
Quoted 10 lines
samples/bpf/xdp_stat_kern.c new file +10
-0+/* NR is used to map interceptors to the programs that are being intercepted. */ +#define INTERCEPTOR (INDEX ) \ + SEC("xdp/interceptor_" #INDEX) \ + int interceptor_##INDEX(struct xdp_md *ctx) \ + { \ + return interceptor_impl(ctx, INDEX); \ + } + +/* Required to use bpf_ktime_get_ns() */ + char _license[] SEC("license") = "GPL";
Wrong indentation?
samples/bpf/xdp_stat_kern.c line 50
Quoted 10 lines
samples/bpf/xdp_stat_kern.c new file +10
-0+ +/* interception_info holds a single record per CPU to pass global state between +* interceptor programs. +*/ +struct bpf_map_def SEC("maps") interception_info = { + .type = BPF_MAP_TYPE_PERCPU_ARRAY, + .key_size = sizeof(__u32), + .value_size = sizeof(struct interception_info_rec), + .max_entries = 1, +};
With BTF-define maps this would be
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, struct interception_info_rec);
} interception_info SEC(".maps");Can you please switch all the maps to this new syntax?
Quoted 12 lines
samples/bpf/xdp_stat_kern.c new file +12
-0+ +/* interceptor_stats maps interceptor indexes to measurements of an intercepted +* BPF program. Index 0 maps the interceptor entrypoint to measurements of the +* original entrypoint. +*/ +struct bpf_map_def SEC("maps") prog_stats = { + .type = BPF_MAP_TYPE_PERCPU_ARRAY, + .key_size = sizeof(__u32), + .value_size = sizeof(struct prog_stats_rec), + .max_entries = MAX_PROG_ARRAY, +}; +
[...]
samples/bpf/xdp_stat_user.c line 458
Quoted 13 lines
samples/bpf/xdp_stat_user.c new file +13
-0+static struct intercept_ctx *intercept__setup (char *mappath,int ifindex) +{ +struct intercept_ctx *ctx =malloc (sizeof (struct intercept_ctx )); + + ctx->ifindex = ifindex; + + ctx->stats_enabled_oldval = +_update_sysctl ("/proc/sys/kernel/bpf_stats_enabled" ,1 ); +if (ctx->stats_enabled_oldval <0 ) +perror ("ERR: set bpf_stats_enabled sysctl failed\n" ); + +if (bpf_prog_load (FILENAME_XDP_STAT_KERN ,BPF_PROG_TYPE_XDP , + &ctx->bpf_obj, &ctx->entry_fd)) {
bpf_object__open() + bpf_object__load() is preferred over bpf_prog_load() way to open and load BPF object files, would be nice to use that instead.
Alternative is BPF skeleton, but as I said, it will require a bit more set up in Makefile (you'd need to generate skeleton with bpftool gen skeleton <your-bpf-object>.o). Few benefits of BPF skeleton, though:
- you wouldn't need to look up maps and programs by name, they would
be accessible as struct fields of skeleton (e.g., skel->maps.interception_info);
- BPF object file would be embedded into userspace program, so when
distributing this you wouldn't need to drag along a separate .o file.
Quoted 5 lines
samples/bpf/xdp_stat_user.c new file +5
-0+fprintf (stderr,"ERR: failed to load %s\n" , +FILENAME_XDP_STAT_KERN ); +return NULL ; + } +
[...]
PATCH v2 1/1 samples/bpf: Add xdp_stat sample program
In reply to Eric SageAt Facebook we use tail calls to jump between our firewall filters and our L4LB. This is a program I wrote to estimate per program performance by swapping out the entries in the program array with interceptors that take measurements and then jump to the original entries.
I found the sample programs to be invaluable in understanding how to use the libbpf API (as well as the test env from the xdp-tutorial repo for testing), and want to return the favor. I am currently working on my next iteration that uses fentry/fexit to be less invasive, but I thought it was an interesting PoC of what you can do with program arrays.
---
Changes in v2:
- Upped max number of interceptors to 32 and mentioned the max in the help.
- Fixed license formatting.
- Requested change to convert BPF map definitions to BTF.
samples/bpf/Makefile | 3 + samples/bpf/xdp_stat_common.h | 28 ++ samples/bpf/xdp_stat_kern.c | 192 +++++++++ samples/bpf/xdp_stat_user.c | 748 ++++++++++++++++++++++++++++++++++ 4 files changed, 971 insertions(+) create mode 100644 samples/bpf/xdp_stat_common.h create mode 100644 samples/bpf/xdp_stat_kern.c create mode 100644 samples/bpf/xdp_stat_user.c
samples/bpf/Makefile +3 -0
@@ -53,6 +53,7 @@ tprogs-y += task_fd_query tprogs-y += xdp_sample_pkts tprogs-y += ibumad tprogs-y += hbm +tprogs-y += xdp_stat # Libbpf dependencies LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
@@ -109,6 +110,7 @@ task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS) ibumad-objs := bpf_load.o ibumad_user.o $(TRACE_HELPERS) hbm-objs := bpf_load.o hbm.o $(CGROUP_HELPERS) +xdp_stat-objs := xdp_stat_user.o # Tell kbuild to always build the programs always := $(tprogs-y)
@@ -170,6 +172,7 @@ always += ibumad_kern.o always += hbm_out_kern.o always += hbm_edt_kern.o always += xdpsock_kern.o +always += xdp_stat_kern.o ifeq ($(ARCH), arm) # Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
samples/bpf/xdp_stat_common.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 +* Copyright (c) 2019 Facebook +*/ + +#ifndef __XDP_STAT_COMMON_HEADER_H +#define __XDP_STAT_COMMON_HEADER_H + +/* WARNING: Must match the number of interceptors generated in +* xdp_stat_kern.c including xdp/interceptor_0. +*/ +#define MAX_INTERCEPTORS 10 + +/* prog_stats_rec contains stats specific to an intercepted BPF program. +*/ +struct prog_stats_rec { +__u64 nr_terminal_runs; +__u64 nr_chained_runs; +__u64 ns_chained_runtime; +}; + +/* interception_info_rec contains global to all interceptors on a single CPU. +*/ +struct interception_info_rec { +__u32 prev_interceptor_nr; +__u64 prev_ns_start; +}; + +#endif /* __XDP_STAT_COMMON_HEADER_H */
samples/bpf/xdp_stat_kern.c new file +192 -0
192 lines not on this page · raw message
samples/bpf/xdp_stat_user.c new file +748 -0
748 lines not on this page · raw message
Signature · 1 line
2.24.1
Quoted 13 lines · Eric Sage
At Facebook we use tail calls to jump between our firewall filters and our L4LB. This is a program I wrote to estimate per program performance by swapping out the entries in the program array with interceptors that take measurements and then jump to the original entries.
I found the sample programs to be invaluable in understanding how to use the libbpf API (as well as the test env from the xdp-tutorial repo for testing), and want to return the favor. I am currently working on my next iteration that uses fentry/fexit to be less invasive, but I thought it was an interesting PoC of what you can do with program arrays.
Quoted 8 lines
Signed-off-by Eric Sage <[email protected]>---
Changes in v2:
- Upped max number of interceptors to 32 and mentioned the max in the help.
- Fixed license formatting.
- Requested change to convert BPF map definitions to BTF.
I should have mentioned a gotcha with PROG_ARRAY earlier, sorry about that. Use key_size/value_size for it. Other than that, looks good.
[...]
Quoted 11 lines
samples/bpf/xdp_stat_kern.c new file +5
-0@@ -0,0 +1,192 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright(c) 2019 Facebook +*/ + +/* Conceptually interception looks like this for a single packet:
samples/bpf/xdp_stat_kern.c line 15
Quoted 10 lines
samples/bpf/xdp_stat_kern.c new file +10
-0+* +* interceptor_0 -> entrypoint -> interceptor_1 -> prog_1 -> ... -> +* interceptor_N -> prog_N -> XDP_ACTION +* +* At any point in the chain, including in the entrypoint, an XDP_ACTION can +* be returned. It is also not assumed that the order of jumps will not change +* (except that the entrypoint always comes first). +* +* Because there is no way to hook into the return of the XDP action, the +* entrpoint (interceptor_0) is also used to record the terminal run of the
typo: entrypoint
samples/bpf/xdp_stat_kern.c line 16
Quoted 1 line
samples/bpf/xdp_stat_kern.c new file +1
-0+* previous BPF program on the same CPU. Conceputally:
typo: Conceptually
Quoted 7 lines
samples/bpf/xdp_stat_kern.c new file +7
-0+* +* ... -> prog_N -> XDP_ACTION -> interceptor_0 -> ... +* +* FIXME: A bad side effect of this is that the reported stats will always be +* behind in tracking terminal runs which is confusing to the user. +*/ +
[...]
samples/bpf/xdp_stat_kern.c line 81
Quoted 8 lines
samples/bpf/xdp_stat_kern.c new file +8
-0+/* jmp_table_entry has a single entry - the original XDP entrpoint - so that +* the intercpetor entrypoint can jump to it. +*/ +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32);
Did this actually work, when you run your tool? Few special maps still don't support specifying type information, I think PROG_ARRAY is one of those. For such cases you have to specify key_size and value_size, instead of key/value types:
__uint(key_size, sizeof(__u32));__uint(value_size, sizeof(__u32));
Quoted 2 lines
samples/bpf/xdp_stat_kern.c new file +2
-0+} jmp_table_entrypoint SEC(".maps"); +
[...]
PATCH v3 1/1 samples/bpf: Add xdp_stat sample program
At Facebook we use tail calls to jump between our firewall filters and our L4LB. This is a program I wrote to estimate per program performance by swapping out the entries in the program array with interceptors that take measurements and then jump to the original entries.
I found the sample programs to be invaluable in understanding how to use the libbpf API (as well as the test env from the xdp-tutorial repo for testing), and want to return the favor. I am currently working on my next iteration that uses fentry/fexit to be less invasive, but I thought it was an interesting PoC of what you can do with program arrays.
---
Changes in v3:
- Fixed typos in xdp_stat_kern.c
- Switch to using key_size, value_size for prog arrays.
samples/bpf/Makefile | 3 + samples/bpf/xdp_stat_common.h | 28 ++ samples/bpf/xdp_stat_kern.c | 192 +++++++++ samples/bpf/xdp_stat_user.c | 748 ++++++++++++++++++++++++++++++++++ 4 files changed, 971 insertions(+) create mode 100644 samples/bpf/xdp_stat_common.h create mode 100644 samples/bpf/xdp_stat_kern.c create mode 100644 samples/bpf/xdp_stat_user.c
samples/bpf/Makefile +3 -0
@@ -53,6 +53,7 @@ tprogs-y += task_fd_query tprogs-y += xdp_sample_pkts tprogs-y += ibumad tprogs-y += hbm +tprogs-y += xdp_stat # Libbpf dependencies LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
@@ -109,6 +110,7 @@ task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS) ibumad-objs := bpf_load.o ibumad_user.o $(TRACE_HELPERS) hbm-objs := bpf_load.o hbm.o $(CGROUP_HELPERS) +xdp_stat-objs := xdp_stat_user.o # Tell kbuild to always build the programs always := $(tprogs-y)
@@ -170,6 +172,7 @@ always += ibumad_kern.o always += hbm_out_kern.o always += hbm_edt_kern.o always += xdpsock_kern.o +always += xdp_stat_kern.o ifeq ($(ARCH), arm) # Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
samples/bpf/xdp_stat_common.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 +* Copyright (c) 2019 Facebook +*/ + +#ifndef __XDP_STAT_COMMON_HEADER_H +#define __XDP_STAT_COMMON_HEADER_H + +/* WARNING: Must match the number of interceptors generated in +* xdp_stat_kern.c including xdp/interceptor_0. +*/ +#define MAX_INTERCEPTORS 10 + +/* prog_stats_rec contains stats specific to an intercepted BPF program. +*/ +struct prog_stats_rec { +__u64 nr_terminal_runs; +__u64 nr_chained_runs; +__u64 ns_chained_runtime; +}; + +/* interception_info_rec contains global to all interceptors on a single CPU. +*/ +struct interception_info_rec { +__u32 prev_interceptor_nr; +__u64 prev_ns_start; +}; + +#endif /* __XDP_STAT_COMMON_HEADER_H */
samples/bpf/xdp_stat_kern.c new file +192 -0
192 lines not on this page · raw message
samples/bpf/xdp_stat_user.c new file +748 -0
748 lines not on this page · raw message
Signature · 1 line
2.24.1
Quoted 13 lines · Eric Sage
At Facebook we use tail calls to jump between our firewall filters and our L4LB. This is a program I wrote to estimate per program performance by swapping out the entries in the program array with interceptors that take measurements and then jump to the original entries.
I found the sample programs to be invaluable in understanding how to use the libbpf API (as well as the test env from the xdp-tutorial repo for testing), and want to return the favor. I am currently working on my next iteration that uses fentry/fexit to be less invasive, but I thought it was an interesting PoC of what you can do with program arrays.
Quoted 2 lines
Signed-off-by Eric Sage <[email protected]>---
Looks good, thanks!
Quoted 4 lines
Changes in v3:
- Fixed typos inxdp_stat_kern.c
- Switch to usingkey_size,value_sizefor prog arrays.
Just for the future: you should keep entire changelog, starting from v1->v2 changes. Also, netdev patches usually preserve them as part of commit message (for individual patch) or cover letter (for patch set). But in this case I think changes are not substantial enough to record them permanently, so it's fine.
[...]
Daniel Borkmann
maintainerQuoted 5 lines · Eric Sage
At Facebook we use tail calls to jump between our firewall filters and our L4LB. This is a program I wrote to estimate per program performance by swapping out the entries in the program array with interceptors that take measurements and then jump to the original entries.
Quoted 8 lines
I found the sample programs to be invaluable in understanding how to use the libbpf API (as well as the test env from the xdp-tutorial repo for testing), and want to return the favor. I am currently working on my next iteration that uses fentry/fexit to be less invasive, but I thought it was an interesting PoC of what you can do with program arrays.
Signed-off-by Eric Sage <[email protected]>
Now that bpf-next is back open, this needs a rebase for proceeding to get merged.
Thanks,
Daniel