Skip to content

Commit 080a70b

Browse files
author
Alexei Starovoitov
committed
Merge branch 'Deprecate bpf_prog_load_xattr() API'
Andrii Nakryiko says: ==================== Few lines in the last patch to mark bpf_prog_load_xattr() deprecated required a decent amount of clean ups in all the other patches. samples/bpf is big part of the clean up. This patch set also bumps libbpf version to 0.7, as libbpf v0.6 release will be cut shortly. ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 8b4ff5f + c93faaa commit 080a70b

37 files changed

+293
-202
lines changed

samples/bpf/Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
328328
$(src)/*.c: verify_target_bpf $(LIBBPF)
329329

330330
libbpf_hdrs: $(LIBBPF)
331-
$(obj)/$(TRACE_HELPERS): | libbpf_hdrs
331+
$(obj)/$(TRACE_HELPERS) $(obj)/$(CGROUP_HELPERS) $(obj)/$(XDP_SAMPLE): | libbpf_hdrs
332332

333333
.PHONY: libbpf_hdrs
334334

@@ -343,6 +343,17 @@ $(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
343343
$(obj)/hbm.o: $(src)/hbm.h
344344
$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
345345

346+
# Override includes for xdp_sample_user.o because $(srctree)/usr/include in
347+
# TPROGS_CFLAGS causes conflicts
348+
XDP_SAMPLE_CFLAGS += -Wall -O2 -lm \
349+
-I$(src)/../../tools/include \
350+
-I$(src)/../../tools/include/uapi \
351+
-I$(LIBBPF_INCLUDE) \
352+
-I$(src)/../../tools/testing/selftests/bpf
353+
354+
$(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS)
355+
$(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h
356+
346357
-include $(BPF_SAMPLES_PATH)/Makefile.target
347358

348359
VMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux)) \

samples/bpf/Makefile.target

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,3 @@ quiet_cmd_tprog-cobjs = CC $@
7373
cmd_tprog-cobjs = $(CC) $(tprogc_flags) -c -o $@ $<
7474
$(tprog-cobjs): $(obj)/%.o: $(src)/%.c FORCE
7575
$(call if_changed_dep,tprog-cobjs)
76-
77-
# Override includes for xdp_sample_user.o because $(srctree)/usr/include in
78-
# TPROGS_CFLAGS causes conflicts
79-
XDP_SAMPLE_CFLAGS += -Wall -O2 -lm \
80-
-I./tools/include \
81-
-I./tools/include/uapi \
82-
-I./tools/lib \
83-
-I./tools/testing/selftests/bpf
84-
$(obj)/xdp_sample_user.o: $(src)/xdp_sample_user.c \
85-
$(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h
86-
$(CC) $(XDP_SAMPLE_CFLAGS) -c -o $@ $<

samples/bpf/cookie_uid_helper_example.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ static bool test_finish;
6767

6868
static void maps_create(void)
6969
{
70-
map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(uint32_t),
71-
sizeof(struct stats), 100, 0);
70+
map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(uint32_t),
71+
sizeof(struct stats), 100, NULL);
7272
if (map_fd < 0)
7373
error(1, errno, "map create failed!\n");
7474
}
@@ -157,9 +157,13 @@ static void prog_load(void)
157157
offsetof(struct __sk_buff, len)),
158158
BPF_EXIT_INSN(),
159159
};
160-
prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
161-
ARRAY_SIZE(prog), "GPL", 0,
162-
log_buf, sizeof(log_buf));
160+
LIBBPF_OPTS(bpf_prog_load_opts, opts,
161+
.log_buf = log_buf,
162+
.log_size = sizeof(log_buf),
163+
);
164+
165+
prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
166+
prog, ARRAY_SIZE(prog), &opts);
163167
if (prog_fd < 0)
164168
error(1, errno, "failed to load prog\n%s\n", log_buf);
165169
}

samples/bpf/fds_example.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,22 @@ static int bpf_prog_create(const char *object)
5454
};
5555
size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
5656
struct bpf_object *obj;
57-
int prog_fd;
57+
int err;
5858

5959
if (object) {
60-
assert(!bpf_prog_load(object, BPF_PROG_TYPE_UNSPEC,
61-
&obj, &prog_fd));
62-
return prog_fd;
60+
obj = bpf_object__open_file(object, NULL);
61+
assert(!libbpf_get_error(obj));
62+
err = bpf_object__load(obj);
63+
assert(!err);
64+
return bpf_program__fd(bpf_object__next_program(obj, NULL));
6365
} else {
64-
return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
65-
insns, insns_cnt, "GPL", 0,
66-
bpf_log_buf, BPF_LOG_BUF_SIZE);
66+
LIBBPF_OPTS(bpf_prog_load_opts, opts,
67+
.log_buf = bpf_log_buf,
68+
.log_size = BPF_LOG_BUF_SIZE,
69+
);
70+
71+
return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
72+
insns, insns_cnt, &opts);
6773
}
6874
}
6975

@@ -73,8 +79,8 @@ static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
7379
int fd, ret;
7480

7581
if (flags & BPF_F_PIN) {
76-
fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(uint32_t),
77-
sizeof(uint32_t), 1024, 0);
82+
fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(uint32_t),
83+
sizeof(uint32_t), 1024, NULL);
7884
printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
7985
assert(fd > 0);
8086

samples/bpf/hbm_kern.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
* Include file for sample Host Bandwidth Manager (HBM) BPF programs
1010
*/
1111
#define KBUILD_MODNAME "foo"
12-
#include <stddef.h>
13-
#include <stdbool.h>
1412
#include <uapi/linux/bpf.h>
1513
#include <uapi/linux/if_ether.h>
1614
#include <uapi/linux/if_packet.h>

samples/bpf/lwt_len_hist_kern.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
#include <uapi/linux/in.h>
1717
#include <bpf/bpf_helpers.h>
1818

19-
# define printk(fmt, ...) \
20-
({ \
21-
char ____fmt[] = fmt; \
22-
bpf_trace_printk(____fmt, sizeof(____fmt), \
23-
##__VA_ARGS__); \
24-
})
25-
2619
struct bpf_elf_map {
2720
__u32 type;
2821
__u32 size_key;

samples/bpf/map_perf_test_user.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,22 @@ static void do_test_lru(enum test_type test, int cpu)
134134
*/
135135
int outer_fd = map_fd[array_of_lru_hashs_idx];
136136
unsigned int mycpu, mynode;
137+
LIBBPF_OPTS(bpf_map_create_opts, opts,
138+
.map_flags = BPF_F_NUMA_NODE,
139+
);
137140

138141
assert(cpu < MAX_NR_CPUS);
139142

140143
ret = syscall(__NR_getcpu, &mycpu, &mynode, NULL);
141144
assert(!ret);
142145

146+
opts.numa_node = mynode;
143147
inner_lru_map_fds[cpu] =
144-
bpf_create_map_node(BPF_MAP_TYPE_LRU_HASH,
145-
test_map_names[INNER_LRU_HASH_PREALLOC],
146-
sizeof(uint32_t),
147-
sizeof(long),
148-
inner_lru_hash_size, 0,
149-
mynode);
148+
bpf_map_create(BPF_MAP_TYPE_LRU_HASH,
149+
test_map_names[INNER_LRU_HASH_PREALLOC],
150+
sizeof(uint32_t),
151+
sizeof(long),
152+
inner_lru_hash_size, &opts);
150153
if (inner_lru_map_fds[cpu] == -1) {
151154
printf("cannot create BPF_MAP_TYPE_LRU_HASH %s(%d)\n",
152155
strerror(errno), errno);

samples/bpf/sock_example.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ static int test_sock(void)
3737
int sock = -1, map_fd, prog_fd, i, key;
3838
long long value = 0, tcp_cnt, udp_cnt, icmp_cnt;
3939

40-
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
41-
256, 0);
40+
map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key), sizeof(value),
41+
256, NULL);
4242
if (map_fd < 0) {
4343
printf("failed to create map '%s'\n", strerror(errno));
4444
goto cleanup;
@@ -59,9 +59,13 @@ static int test_sock(void)
5959
BPF_EXIT_INSN(),
6060
};
6161
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
62+
LIBBPF_OPTS(bpf_prog_load_opts, opts,
63+
.log_buf = bpf_log_buf,
64+
.log_size = BPF_LOG_BUF_SIZE,
65+
);
6266

63-
prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, insns_cnt,
64-
"GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE);
67+
prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
68+
prog, insns_cnt, &opts);
6569
if (prog_fd < 0) {
6670
printf("failed to load prog '%s'\n", strerror(errno));
6771
goto cleanup;

samples/bpf/sockex1_user.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,26 @@
1111
int main(int ac, char **argv)
1212
{
1313
struct bpf_object *obj;
14+
struct bpf_program *prog;
1415
int map_fd, prog_fd;
1516
char filename[256];
16-
int i, sock;
17+
int i, sock, err;
1718
FILE *f;
1819

1920
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
2021

21-
if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
22-
&obj, &prog_fd))
22+
obj = bpf_object__open_file(filename, NULL);
23+
if (libbpf_get_error(obj))
2324
return 1;
2425

26+
prog = bpf_object__next_program(obj, NULL);
27+
bpf_program__set_type(prog, BPF_PROG_TYPE_SOCKET_FILTER);
28+
29+
err = bpf_object__load(obj);
30+
if (err)
31+
return 1;
32+
33+
prog_fd = bpf_program__fd(prog);
2534
map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
2635

2736
sock = open_raw_sock("lo");

samples/bpf/sockex2_user.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,26 @@ struct pair {
1616

1717
int main(int ac, char **argv)
1818
{
19+
struct bpf_program *prog;
1920
struct bpf_object *obj;
2021
int map_fd, prog_fd;
2122
char filename[256];
22-
int i, sock;
23+
int i, sock, err;
2324
FILE *f;
2425

2526
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
27+
obj = bpf_object__open_file(filename, NULL);
28+
if (libbpf_get_error(obj))
29+
return 1;
30+
31+
prog = bpf_object__next_program(obj, NULL);
32+
bpf_program__set_type(prog, BPF_PROG_TYPE_SOCKET_FILTER);
2633

27-
if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
28-
&obj, &prog_fd))
34+
err = bpf_object__load(obj);
35+
if (err)
2936
return 1;
3037

38+
prog_fd = bpf_program__fd(prog);
3139
map_fd = bpf_object__find_map_fd_by_name(obj, "hash_map");
3240

3341
sock = open_raw_sock("lo");

0 commit comments

Comments
 (0)