From 1946d2c9ee88457b59de7e06399be1a7e2b803a8 Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Mon, 3 May 2021 10:32:34 +0800 Subject: [PATCH 1/3] basic: code cleanup Signed-off-by: Hengqi Chen --- basic01-xdp-pass/xdp_pass_user.c | 2 +- basic03-map-counter/xdp_load_and_stats.c | 6 +++--- basic04-pinning-maps/xdp_prog_kern.c | 7 ------- basic04-pinning-maps/xdp_stats.c | 2 +- 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/basic01-xdp-pass/xdp_pass_user.c b/basic01-xdp-pass/xdp_pass_user.c index 41b4ba27..f02d9621 100644 --- a/basic01-xdp-pass/xdp_pass_user.c +++ b/basic01-xdp-pass/xdp_pass_user.c @@ -162,7 +162,7 @@ int main(int argc, char **argv) if (err) return err; - /* This step is not really needed , BPF-info via bpf-syscall */ + /* This step is not really needed , BPF-info via bpf-syscall */ err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); if (err) { fprintf(stderr, "ERR: can't get prog info - %s\n", diff --git a/basic03-map-counter/xdp_load_and_stats.c b/basic03-map-counter/xdp_load_and_stats.c index b4142e8b..e9399cc3 100644 --- a/basic03-map-counter/xdp_load_and_stats.c +++ b/basic03-map-counter/xdp_load_and_stats.c @@ -67,7 +67,7 @@ int find_map_fd(struct bpf_object *bpf_obj, const char *mapname) /* Lesson#3: bpf_object to bpf_map */ map = bpf_object__find_map_by_name(bpf_obj, mapname); - if (!map) { + if (!map) { fprintf(stderr, "ERR: cannot find map by name: %s\n", mapname); goto out; } @@ -85,7 +85,7 @@ static __u64 gettime(void) res = clock_gettime(CLOCK_MONOTONIC, &t); if (res < 0) { - fprintf(stderr, "Error with gettimeofday! (%i)\n", res); + fprintf(stderr, "Error with clock_gettime! (%i)\n", res); exit(EXIT_FAIL); } return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec; @@ -131,7 +131,7 @@ static void stats_print(struct stats_record *stats_rec, period = calc_period(rec, prev); if (period == 0) - return; + return; packets = rec->total.rx_packets - prev->total.rx_packets; pps = packets / period; diff --git a/basic04-pinning-maps/xdp_prog_kern.c b/basic04-pinning-maps/xdp_prog_kern.c index 36c3885b..57c01419 100644 --- a/basic04-pinning-maps/xdp_prog_kern.c +++ b/basic04-pinning-maps/xdp_prog_kern.c @@ -15,13 +15,6 @@ struct bpf_map_def SEC("maps") xdp_stats_map = { .max_entries = XDP_ACTION_MAX, }; -/* LLVM maps __sync_fetch_and_add() as a built-in function to the BPF atomic add - * instruction (that is BPF_STX | BPF_XADD | BPF_W for word sizes) - */ -#ifndef lock_xadd -#define lock_xadd(ptr, val) ((void) __sync_fetch_and_add(ptr, val)) -#endif - static __always_inline __u32 xdp_stats_record_action(struct xdp_md *ctx, __u32 action) { diff --git a/basic04-pinning-maps/xdp_stats.c b/basic04-pinning-maps/xdp_stats.c index 6bb46ad3..2aa0fea4 100644 --- a/basic04-pinning-maps/xdp_stats.c +++ b/basic04-pinning-maps/xdp_stats.c @@ -46,7 +46,7 @@ static __u64 gettime(void) res = clock_gettime(CLOCK_MONOTONIC, &t); if (res < 0) { - fprintf(stderr, "Error with gettimeofday! (%i)\n", res); + fprintf(stderr, "Error with clock_gettime! (%i)\n", res); exit(EXIT_FAIL); } return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec; From e37075dd3bcc844f534c94f9568a02e8e6ab83bc Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Mon, 3 May 2021 10:53:35 +0800 Subject: [PATCH 2/3] libbpf: sync with latest libbpf Signed-off-by: Hengqi Chen --- common/common.mk | 2 +- libbpf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/common.mk b/common/common.mk index df1afc1a..3df6af48 100644 --- a/common/common.mk +++ b/common/common.mk @@ -45,7 +45,7 @@ LDFLAGS ?= -L$(LIBBPF_DIR) BPF_CFLAGS ?= -I$(LIBBPF_DIR)/build/usr/include/ -I../headers/ -LIBS = -l:libbpf.a -lelf $(USER_LIBS) +LIBS = -l:libbpf.a -lelf -lz $(USER_LIBS) all: llvm-check $(USER_TARGETS) $(XDP_OBJ) $(COPY_LOADER) $(COPY_STATS) diff --git a/libbpf b/libbpf index b91f53ec..9e123fa5 160000 --- a/libbpf +++ b/libbpf @@ -1 +1 @@ -Subproject commit b91f53ec5f1aba2a9d01dc00c4434063abd921e8 +Subproject commit 9e123fa5d20017923ec39b5af5f269488b7073d6 From 9586e39ce554f196acefec316ed5693a4e95f41b Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Mon, 3 May 2021 10:54:57 +0800 Subject: [PATCH 3/3] replace deprecated bpf_program__title with bpf_program__section_name Signed-off-by: Hengqi Chen --- basic02-prog-by-name/xdp_loader.c | 2 +- common/common_user_bpf_xdp.c | 2 +- tracing02-xdp-monitor/trace_load_and_stats.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/basic02-prog-by-name/xdp_loader.c b/basic02-prog-by-name/xdp_loader.c index e18d97fa..fc2c008b 100644 --- a/basic02-prog-by-name/xdp_loader.c +++ b/basic02-prog-by-name/xdp_loader.c @@ -151,7 +151,7 @@ static void list_avail_progs(struct bpf_object *obj) bpf_object__for_each_program(pos, obj) { if (bpf_program__is_xdp(pos)) - printf(" %s\n", bpf_program__title(pos, false)); + printf(" %s\n", bpf_program__section_name(pos)); } } diff --git a/common/common_user_bpf_xdp.c b/common/common_user_bpf_xdp.c index e7ef7717..faf7f4f9 100644 --- a/common/common_user_bpf_xdp.c +++ b/common/common_user_bpf_xdp.c @@ -274,7 +274,7 @@ struct bpf_object *load_bpf_and_xdp_attach(struct config *cfg) exit(EXIT_FAIL_BPF); } - strncpy(cfg->progsec, bpf_program__title(bpf_prog, false), sizeof(cfg->progsec)); + strncpy(cfg->progsec, bpf_program__section_name(bpf_prog), sizeof(cfg->progsec)); prog_fd = bpf_program__fd(bpf_prog); if (prog_fd <= 0) { diff --git a/tracing02-xdp-monitor/trace_load_and_stats.c b/tracing02-xdp-monitor/trace_load_and_stats.c index a7b75aaa..4c0e2dcb 100644 --- a/tracing02-xdp-monitor/trace_load_and_stats.c +++ b/tracing02-xdp-monitor/trace_load_and_stats.c @@ -796,7 +796,7 @@ static struct bpf_object* load_bpf_and_trace_attach(struct config *cfg) } bpf_object__for_each_program(prog, obj) { - const char *sec = bpf_program__title(prog, true); + const char *sec = bpf_program__section_name(prog); char *tp; if (!sec) {