Skip to content

Commit a84880e

Browse files
committed
Merge branch 'bpf-perf-rb-libbpf'
Jakub Kicinski says: ==================== This series started out as a follow up to the bpftool perf event dumping patches. As suggested by Daniel patch 1 makes use of PERF_SAMPLE_TIME to simplify code and improve accuracy of timestamps. Remaining patches are trying to move perf event loop into libbpf as suggested by Alexei. One user for this new function is bpftool which links with libbpf nicely, the other, unfortunately, is in samples/bpf. Remaining patches make samples/bpf link against full libbpf.a (not just a handful of objects). Once we have full power of libbpf at our disposal we can convert some of XDP samples to use libbpf loader instead of bpf_load.c. My understanding is that this is the desired direction, at least for networking code. ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2 parents cb9c28e + be5bca4 commit a84880e

File tree

15 files changed

+307
-222
lines changed

15 files changed

+307
-222
lines changed

samples/bpf/Makefile

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ hostprogs-y += xdpsock
4949
hostprogs-y += xdp_fwd
5050

5151
# Libbpf dependencies
52-
LIBBPF := ../../tools/lib/bpf/bpf.o ../../tools/lib/bpf/nlattr.o
52+
LIBBPF := ../../tools/lib/bpf/libbpf.a
5353
CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
5454
TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
5555

@@ -74,14 +74,14 @@ offwaketime-objs := bpf_load.o $(LIBBPF) offwaketime_user.o $(TRACE_HELPERS)
7474
spintest-objs := bpf_load.o $(LIBBPF) spintest_user.o $(TRACE_HELPERS)
7575
map_perf_test-objs := bpf_load.o $(LIBBPF) map_perf_test_user.o
7676
test_overhead-objs := bpf_load.o $(LIBBPF) test_overhead_user.o
77-
test_cgrp2_array_pin-objs := $(LIBBPF) test_cgrp2_array_pin.o
78-
test_cgrp2_attach-objs := $(LIBBPF) test_cgrp2_attach.o
79-
test_cgrp2_attach2-objs := $(LIBBPF) test_cgrp2_attach2.o $(CGROUP_HELPERS)
80-
test_cgrp2_sock-objs := $(LIBBPF) test_cgrp2_sock.o
77+
test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o $(LIBBPF)
78+
test_cgrp2_attach-objs := test_cgrp2_attach.o $(LIBBPF)
79+
test_cgrp2_attach2-objs := test_cgrp2_attach2.o $(LIBBPF) $(CGROUP_HELPERS)
80+
test_cgrp2_sock-objs := test_cgrp2_sock.o $(LIBBPF)
8181
test_cgrp2_sock2-objs := bpf_load.o $(LIBBPF) test_cgrp2_sock2.o
82-
xdp1-objs := bpf_load.o $(LIBBPF) xdp1_user.o
82+
xdp1-objs := xdp1_user.o $(LIBBPF)
8383
# reuse xdp1 source intentionally
84-
xdp2-objs := bpf_load.o $(LIBBPF) xdp1_user.o
84+
xdp2-objs := xdp1_user.o $(LIBBPF)
8585
xdp_router_ipv4-objs := bpf_load.o $(LIBBPF) xdp_router_ipv4_user.o
8686
test_current_task_under_cgroup-objs := bpf_load.o $(LIBBPF) $(CGROUP_HELPERS) \
8787
test_current_task_under_cgroup_user.o
@@ -91,15 +91,15 @@ tc_l2_redirect-objs := bpf_load.o $(LIBBPF) tc_l2_redirect_user.o
9191
lwt_len_hist-objs := bpf_load.o $(LIBBPF) lwt_len_hist_user.o
9292
xdp_tx_iptunnel-objs := bpf_load.o $(LIBBPF) xdp_tx_iptunnel_user.o
9393
test_map_in_map-objs := bpf_load.o $(LIBBPF) test_map_in_map_user.o
94-
per_socket_stats_example-objs := $(LIBBPF) cookie_uid_helper_example.o
94+
per_socket_stats_example-objs := cookie_uid_helper_example.o $(LIBBPF)
9595
xdp_redirect-objs := bpf_load.o $(LIBBPF) xdp_redirect_user.o
9696
xdp_redirect_map-objs := bpf_load.o $(LIBBPF) xdp_redirect_map_user.o
9797
xdp_redirect_cpu-objs := bpf_load.o $(LIBBPF) xdp_redirect_cpu_user.o
9898
xdp_monitor-objs := bpf_load.o $(LIBBPF) xdp_monitor_user.o
99-
xdp_rxq_info-objs := bpf_load.o $(LIBBPF) xdp_rxq_info_user.o
99+
xdp_rxq_info-objs := xdp_rxq_info_user.o $(LIBBPF)
100100
syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o
101101
cpustat-objs := bpf_load.o $(LIBBPF) cpustat_user.o
102-
xdp_adjust_tail-objs := bpf_load.o $(LIBBPF) xdp_adjust_tail_user.o
102+
xdp_adjust_tail-objs := xdp_adjust_tail_user.o $(LIBBPF)
103103
xdpsock-objs := bpf_load.o $(LIBBPF) xdpsock_user.o
104104
xdp_fwd-objs := bpf_load.o $(LIBBPF) xdp_fwd_user.o
105105

@@ -165,6 +165,16 @@ HOSTCFLAGS += -I$(srctree)/tools/lib/ -I$(srctree)/tools/include
165165
HOSTCFLAGS += -I$(srctree)/tools/perf
166166

167167
HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable
168+
HOSTCFLAGS_trace_helpers.o += -I$(srctree)/tools/lib/bpf/
169+
170+
HOSTCFLAGS_trace_output_user.o += -I$(srctree)/tools/lib/bpf/
171+
HOSTCFLAGS_offwaketime_user.o += -I$(srctree)/tools/lib/bpf/
172+
HOSTCFLAGS_spintest_user.o += -I$(srctree)/tools/lib/bpf/
173+
HOSTCFLAGS_trace_event_user.o += -I$(srctree)/tools/lib/bpf/
174+
HOSTCFLAGS_sampleip_user.o += -I$(srctree)/tools/lib/bpf/
175+
176+
HOSTLOADLIBES_test_lru_dist += -lelf
177+
HOSTLOADLIBES_sock_example += -lelf
168178
HOSTLOADLIBES_fds_example += -lelf
169179
HOSTLOADLIBES_sockex1 += -lelf
170180
HOSTLOADLIBES_sockex2 += -lelf
@@ -176,6 +186,10 @@ HOSTLOADLIBES_tracex4 += -lelf -lrt
176186
HOSTLOADLIBES_tracex5 += -lelf
177187
HOSTLOADLIBES_tracex6 += -lelf
178188
HOSTLOADLIBES_tracex7 += -lelf
189+
HOSTLOADLIBES_test_cgrp2_array_pin += -lelf
190+
HOSTLOADLIBES_test_cgrp2_attach += -lelf
191+
HOSTLOADLIBES_test_cgrp2_attach2 += -lelf
192+
HOSTLOADLIBES_test_cgrp2_sock += -lelf
179193
HOSTLOADLIBES_test_cgrp2_sock2 += -lelf
180194
HOSTLOADLIBES_load_sock_ops += -lelf
181195
HOSTLOADLIBES_test_probe_write_user += -lelf
@@ -195,6 +209,7 @@ HOSTLOADLIBES_tc_l2_redirect += -l elf
195209
HOSTLOADLIBES_lwt_len_hist += -l elf
196210
HOSTLOADLIBES_xdp_tx_iptunnel += -lelf
197211
HOSTLOADLIBES_test_map_in_map += -lelf
212+
HOSTLOADLIBES_per_socket_stats_example += -lelf
198213
HOSTLOADLIBES_xdp_redirect += -lelf
199214
HOSTLOADLIBES_xdp_redirect_map += -lelf
200215
HOSTLOADLIBES_xdp_redirect_cpu += -lelf
@@ -226,7 +241,7 @@ clean:
226241
@rm -f *~
227242

228243
$(LIBBPF): FORCE
229-
$(MAKE) -C $(dir $@) $(notdir $@)
244+
$(MAKE) -C $(dir $@)
230245

231246
$(obj)/syscall_nrs.s: $(src)/syscall_nrs.c
232247
$(call if_changed_dep,cc_s_c)

samples/bpf/bpf_load.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
420420

421421
/* Keeping compatible with ELF maps section changes
422422
* ------------------------------------------------
423-
* The program size of struct bpf_map_def is known by loader
423+
* The program size of struct bpf_load_map_def is known by loader
424424
* code, but struct stored in ELF file can be different.
425425
*
426426
* Unfortunately sym[i].st_size is zero. To calculate the
@@ -429,7 +429,7 @@ static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
429429
* symbols.
430430
*/
431431
map_sz_elf = data_maps->d_size / nr_maps;
432-
map_sz_copy = sizeof(struct bpf_map_def);
432+
map_sz_copy = sizeof(struct bpf_load_map_def);
433433
if (map_sz_elf < map_sz_copy) {
434434
/*
435435
* Backward compat, loading older ELF file with
@@ -448,8 +448,8 @@ static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
448448

449449
/* Memcpy relevant part of ELF maps data to loader maps */
450450
for (i = 0; i < nr_maps; i++) {
451+
struct bpf_load_map_def *def;
451452
unsigned char *addr, *end;
452-
struct bpf_map_def *def;
453453
const char *map_name;
454454
size_t offset;
455455

@@ -464,9 +464,9 @@ static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
464464

465465
/* Symbol value is offset into ELF maps section data area */
466466
offset = sym[i].st_value;
467-
def = (struct bpf_map_def *)(data_maps->d_buf + offset);
467+
def = (struct bpf_load_map_def *)(data_maps->d_buf + offset);
468468
maps[i].elf_offset = offset;
469-
memset(&maps[i].def, 0, sizeof(struct bpf_map_def));
469+
memset(&maps[i].def, 0, sizeof(struct bpf_load_map_def));
470470
memcpy(&maps[i].def, def, map_sz_copy);
471471

472472
/* Verify no newer features were requested */

samples/bpf/bpf_load.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define MAX_MAPS 32
88
#define MAX_PROGS 32
99

10-
struct bpf_map_def {
10+
struct bpf_load_map_def {
1111
unsigned int type;
1212
unsigned int key_size;
1313
unsigned int value_size;
@@ -21,7 +21,7 @@ struct bpf_map_data {
2121
int fd;
2222
char *name;
2323
size_t elf_offset;
24-
struct bpf_map_def def;
24+
struct bpf_load_map_def def;
2525
};
2626

2727
typedef void (*fixup_map_cb)(struct bpf_map_data *map, int idx);

samples/bpf/trace_output_user.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ static int print_bpf_output(void *data, int size)
4848
if (e->cookie != 0x12345678) {
4949
printf("BUG pid %llx cookie %llx sized %d\n",
5050
e->pid, e->cookie, size);
51-
return PERF_EVENT_ERROR;
51+
return LIBBPF_PERF_EVENT_ERROR;
5252
}
5353

5454
cnt++;
5555

5656
if (cnt == MAX_CNT) {
5757
printf("recv %lld events per sec\n",
5858
MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
59-
return PERF_EVENT_DONE;
59+
return LIBBPF_PERF_EVENT_DONE;
6060
}
6161

62-
return PERF_EVENT_CONT;
62+
return LIBBPF_PERF_EVENT_CONT;
6363
}
6464

6565
static void test_bpf_perf_event(void)

samples/bpf/xdp1_user.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#include <libgen.h>
1717
#include <sys/resource.h>
1818

19-
#include "bpf_load.h"
2019
#include "bpf_util.h"
21-
#include "libbpf.h"
20+
#include "bpf/bpf.h"
21+
#include "bpf/libbpf.h"
2222

2323
static int ifindex;
2424
static __u32 xdp_flags;
@@ -31,7 +31,7 @@ static void int_exit(int sig)
3131

3232
/* simple per-protocol drop counter
3333
*/
34-
static void poll_stats(int interval)
34+
static void poll_stats(int map_fd, int interval)
3535
{
3636
unsigned int nr_cpus = bpf_num_possible_cpus();
3737
const unsigned int nr_keys = 256;
@@ -47,7 +47,7 @@ static void poll_stats(int interval)
4747
for (key = 0; key < nr_keys; key++) {
4848
__u64 sum = 0;
4949

50-
assert(bpf_map_lookup_elem(map_fd[0], &key, values) == 0);
50+
assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
5151
for (i = 0; i < nr_cpus; i++)
5252
sum += (values[i] - prev[key][i]);
5353
if (sum)
@@ -71,9 +71,14 @@ static void usage(const char *prog)
7171
int main(int argc, char **argv)
7272
{
7373
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
74+
struct bpf_prog_load_attr prog_load_attr = {
75+
.prog_type = BPF_PROG_TYPE_XDP,
76+
};
7477
const char *optstr = "SN";
78+
int prog_fd, map_fd, opt;
79+
struct bpf_object *obj;
80+
struct bpf_map *map;
7581
char filename[256];
76-
int opt;
7782

7883
while ((opt = getopt(argc, argv, optstr)) != -1) {
7984
switch (opt) {
@@ -102,26 +107,32 @@ int main(int argc, char **argv)
102107
ifindex = strtoul(argv[optind], NULL, 0);
103108

104109
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
110+
prog_load_attr.file = filename;
105111

106-
if (load_bpf_file(filename)) {
107-
printf("%s", bpf_log_buf);
112+
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
113+
return 1;
114+
115+
map = bpf_map__next(NULL, obj);
116+
if (!map) {
117+
printf("finding a map in obj file failed\n");
108118
return 1;
109119
}
120+
map_fd = bpf_map__fd(map);
110121

111-
if (!prog_fd[0]) {
122+
if (!prog_fd) {
112123
printf("load_bpf_file: %s\n", strerror(errno));
113124
return 1;
114125
}
115126

116127
signal(SIGINT, int_exit);
117128
signal(SIGTERM, int_exit);
118129

119-
if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
130+
if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
120131
printf("link set xdp fd failed\n");
121132
return 1;
122133
}
123134

124-
poll_stats(2);
135+
poll_stats(map_fd, 2);
125136

126137
return 0;
127138
}

samples/bpf/xdp_adjust_tail_user.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
#include <netinet/ether.h>
1919
#include <unistd.h>
2020
#include <time.h>
21-
#include "bpf_load.h"
22-
#include "libbpf.h"
23-
#include "bpf_util.h"
21+
#include "bpf/bpf.h"
22+
#include "bpf/libbpf.h"
2423

2524
#define STATS_INTERVAL_S 2U
2625

@@ -36,7 +35,7 @@ static void int_exit(int sig)
3635

3736
/* simple "icmp packet too big sent" counter
3837
*/
39-
static void poll_stats(unsigned int kill_after_s)
38+
static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
4039
{
4140
time_t started_at = time(NULL);
4241
__u64 value = 0;
@@ -46,7 +45,7 @@ static void poll_stats(unsigned int kill_after_s)
4645
while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
4746
sleep(STATS_INTERVAL_S);
4847

49-
assert(bpf_map_lookup_elem(map_fd[0], &key, &value) == 0);
48+
assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
5049

5150
printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
5251
}
@@ -66,14 +65,17 @@ static void usage(const char *cmd)
6665

6766
int main(int argc, char **argv)
6867
{
68+
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
69+
struct bpf_prog_load_attr prog_load_attr = {
70+
.prog_type = BPF_PROG_TYPE_XDP,
71+
};
6972
unsigned char opt_flags[256] = {};
7073
unsigned int kill_after_s = 0;
7174
const char *optstr = "i:T:SNh";
72-
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
75+
int i, prog_fd, map_fd, opt;
76+
struct bpf_object *obj;
77+
struct bpf_map *map;
7378
char filename[256];
74-
int opt;
75-
int i;
76-
7779

7880
for (i = 0; i < strlen(optstr); i++)
7981
if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
@@ -115,26 +117,32 @@ int main(int argc, char **argv)
115117
}
116118

117119
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
120+
prog_load_attr.file = filename;
121+
122+
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
123+
return 1;
118124

119-
if (load_bpf_file(filename)) {
120-
printf("%s", bpf_log_buf);
125+
map = bpf_map__next(NULL, obj);
126+
if (!map) {
127+
printf("finding a map in obj file failed\n");
121128
return 1;
122129
}
130+
map_fd = bpf_map__fd(map);
123131

124-
if (!prog_fd[0]) {
132+
if (!prog_fd) {
125133
printf("load_bpf_file: %s\n", strerror(errno));
126134
return 1;
127135
}
128136

129137
signal(SIGINT, int_exit);
130138
signal(SIGTERM, int_exit);
131139

132-
if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
140+
if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
133141
printf("link set xdp fd failed\n");
134142
return 1;
135143
}
136144

137-
poll_stats(kill_after_s);
145+
poll_stats(map_fd, kill_after_s);
138146

139147
bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
140148

0 commit comments

Comments
 (0)