Skip to content

Commit d40fc18

Browse files
joestringeracmel
authored andcommitted
samples/bpf: Make samples more libbpf-centric
Switch all of the sample code to use the function names from tools/lib/bpf so that they're consistent with that, and to declare their own log buffers. This allow the next commit to be purely devoted to getting rid of the duplicate library in samples/bpf. Committer notes: Testing it: On a fedora rawhide container, with clang/llvm 3.9, sharing the host linux kernel git tree: # make O=/tmp/build/linux/ headers_install # make O=/tmp/build/linux -C samples/bpf/ Since I forgot to make it privileged, just tested it outside the container, using what it generated: # uname -a Linux jouet 4.9.0-rc8+ #1 SMP Mon Dec 12 11:20:49 BRT 2016 x86_64 x86_64 x86_64 GNU/Linux # cd /var/lib/docker/devicemapper/mnt/c43e09a53ff56c86a07baf79847f00e2cc2a17a1e2220e1adbf8cbc62734feda/rootfs/tmp/build/linux/samples/bpf/ # ls -la offwaketime -rwxr-xr-x. 1 root root 24200 Dec 15 12:19 offwaketime # file offwaketime offwaketime: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=c940d3f127d5e66cdd680e42d885cb0b64f8a0e4, not stripped # readelf -SW offwaketime_kern.o | grep PROGBITS [ 2] .text PROGBITS 0000000000000000 000040 000000 00 AX 0 0 4 [ 3] kprobe/try_to_wake_up PROGBITS 0000000000000000 000040 0000d8 00 AX 0 0 8 [ 5] tracepoint/sched/sched_switch PROGBITS 0000000000000000 000118 000318 00 AX 0 0 8 [ 7] maps PROGBITS 0000000000000000 000430 000050 00 WA 0 0 4 [ 8] license PROGBITS 0000000000000000 000480 000004 00 WA 0 0 1 [ 9] version PROGBITS 0000000000000000 000484 000004 00 WA 0 0 4 # ./offwaketime | head -5 swapper/1;start_secondary;cpu_startup_entry;schedule_preempt_disabled;schedule;__schedule;-;---;; 106 CPU 0/KVM;entry_SYSCALL_64_fastpath;sys_ioctl;do_vfs_ioctl;kvm_vcpu_ioctl;kvm_arch_vcpu_ioctl_run;kvm_vcpu_block;schedule;__schedule;-;try_to_wake_up;swake_up_locked;swake_up;apic_timer_expired;apic_timer_fn;__hrtimer_run_queues;hrtimer_interrupt;local_apic_timer_interrupt;smp_apic_timer_interrupt;__irqentry_text_start;cpuidle_enter;call_cpuidle;cpu_startup_entry;start_secondary;;swapper/3 2 Compositor;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;futex_requeue;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;SoftwareVsyncTh 5 firefox;entry_SYSCALL_64_fastpath;sys_poll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;pollwake;__wake_up_common;__wake_up_sync_key;pipe_write;__vfs_write;vfs_write;sys_write;entry_SYSCALL_64_fastpath;;Timer 13 JS Helper;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;firefox 2 # Signed-off-by: Joe Stringer <joe@ovn.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexei Starovoitov <ast@fb.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Wang Nan <wangnan0@huawei.com> Cc: netdev@vger.kernel.org Link: http://lkml.kernel.org/r/20161214224342.12858-2-joe@ovn.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent a5580c7 commit d40fc18

30 files changed

+133
-109
lines changed

samples/bpf/bpf_load.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,33 @@
2222
#include <poll.h>
2323
#include <ctype.h>
2424
#include "libbpf.h"
25-
#include "bpf_helpers.h"
2625
#include "bpf_load.h"
2726

2827
#define DEBUGFS "/sys/kernel/debug/tracing/"
2928

3029
static char license[128];
3130
static int kern_version;
3231
static bool processed_sec[128];
32+
char bpf_log_buf[BPF_LOG_BUF_SIZE];
3333
int map_fd[MAX_MAPS];
3434
int prog_fd[MAX_PROGS];
3535
int event_fd[MAX_PROGS];
3636
int prog_cnt;
3737
int prog_array_fd = -1;
3838

39+
struct bpf_map_def {
40+
unsigned int type;
41+
unsigned int key_size;
42+
unsigned int value_size;
43+
unsigned int max_entries;
44+
unsigned int map_flags;
45+
};
46+
3947
static int populate_prog_array(const char *event, int prog_fd)
4048
{
4149
int ind = atoi(event), err;
4250

43-
err = bpf_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
51+
err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
4452
if (err < 0) {
4553
printf("failed to store prog_fd in prog_array\n");
4654
return -1;
@@ -87,9 +95,10 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
8795
return -1;
8896
}
8997

90-
fd = bpf_prog_load(prog_type, prog, size, license, kern_version);
98+
fd = bpf_load_program(prog_type, prog, size, license, kern_version,
99+
bpf_log_buf, BPF_LOG_BUF_SIZE);
91100
if (fd < 0) {
92-
printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf);
101+
printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
93102
return -1;
94103
}
95104

samples/bpf/bpf_load.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#ifndef __BPF_LOAD_H
22
#define __BPF_LOAD_H
33

4+
#include "libbpf.h"
5+
46
#define MAX_MAPS 32
57
#define MAX_PROGS 32
68

79
extern int map_fd[MAX_MAPS];
810
extern int prog_fd[MAX_PROGS];
911
extern int event_fd[MAX_PROGS];
12+
extern char bpf_log_buf[BPF_LOG_BUF_SIZE];
1013
extern int prog_cnt;
1114

1215
/* parses elf file compiled by llvm .c->.o

samples/bpf/fds_example.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ static int bpf_prog_create(const char *object)
5858
assert(!load_bpf_file((char *)object));
5959
return prog_fd[0];
6060
} else {
61-
return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER,
62-
insns, sizeof(insns), "GPL", 0);
61+
return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
62+
insns, sizeof(insns), "GPL", 0,
63+
bpf_log_buf, BPF_LOG_BUF_SIZE);
6364
}
6465
}
6566

@@ -83,12 +84,12 @@ static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
8384
}
8485

8586
if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
86-
ret = bpf_update_elem(fd, &key, &value, 0);
87+
ret = bpf_map_update_elem(fd, &key, &value, 0);
8788
printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
8889
ret, strerror(errno));
8990
assert(ret == 0);
9091
} else if (flags & BPF_F_KEY) {
91-
ret = bpf_lookup_elem(fd, &key, &value);
92+
ret = bpf_map_lookup_elem(fd, &key, &value);
9293
printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
9394
ret, strerror(errno));
9495
assert(ret == 0);

samples/bpf/lathist_user.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void get_data(int fd)
7373
for (c = 0; c < MAX_CPU; c++) {
7474
for (i = 0; i < MAX_ENTRIES; i++) {
7575
key = c * MAX_ENTRIES + i;
76-
bpf_lookup_elem(fd, &key, &value);
76+
bpf_map_lookup_elem(fd, &key, &value);
7777

7878
cpu_hist[c].data[i] = value;
7979
if (value > cpu_hist[c].max)

samples/bpf/libbpf.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
3232
return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
3333
}
3434

35-
int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
35+
int bpf_map_update_elem(int fd, void *key, void *value, unsigned long long flags)
3636
{
3737
union bpf_attr attr = {
3838
.map_fd = fd,
@@ -44,7 +44,7 @@ int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
4444
return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
4545
}
4646

47-
int bpf_lookup_elem(int fd, void *key, void *value)
47+
int bpf_map_lookup_elem(int fd, void *key, void *value)
4848
{
4949
union bpf_attr attr = {
5050
.map_fd = fd,
@@ -55,7 +55,7 @@ int bpf_lookup_elem(int fd, void *key, void *value)
5555
return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
5656
}
5757

58-
int bpf_delete_elem(int fd, void *key)
58+
int bpf_map_delete_elem(int fd, void *key)
5959
{
6060
union bpf_attr attr = {
6161
.map_fd = fd,
@@ -65,7 +65,7 @@ int bpf_delete_elem(int fd, void *key)
6565
return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
6666
}
6767

68-
int bpf_get_next_key(int fd, void *key, void *next_key)
68+
int bpf_map_get_next_key(int fd, void *key, void *next_key)
6969
{
7070
union bpf_attr attr = {
7171
.map_fd = fd,
@@ -78,19 +78,18 @@ int bpf_get_next_key(int fd, void *key, void *next_key)
7878

7979
#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
8080

81-
char bpf_log_buf[LOG_BUF_SIZE];
82-
83-
int bpf_prog_load(enum bpf_prog_type prog_type,
84-
const struct bpf_insn *insns, int prog_len,
85-
const char *license, int kern_version)
81+
int bpf_load_program(enum bpf_prog_type prog_type,
82+
const struct bpf_insn *insns, int prog_len,
83+
const char *license, int kern_version,
84+
char *log_buf, size_t log_buf_sz)
8685
{
8786
union bpf_attr attr = {
8887
.prog_type = prog_type,
8988
.insns = ptr_to_u64((void *) insns),
9089
.insn_cnt = prog_len / sizeof(struct bpf_insn),
9190
.license = ptr_to_u64((void *) license),
92-
.log_buf = ptr_to_u64(bpf_log_buf),
93-
.log_size = LOG_BUF_SIZE,
91+
.log_buf = ptr_to_u64(log_buf),
92+
.log_size = log_buf_sz,
9493
.log_level = 1,
9594
};
9695

@@ -99,7 +98,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
9998
*/
10099
attr.kern_version = kern_version;
101100

102-
bpf_log_buf[0] = 0;
101+
log_buf[0] = 0;
103102

104103
return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
105104
}

samples/bpf/libbpf.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ struct bpf_insn;
66

77
int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
88
int max_entries, int map_flags);
9-
int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
10-
int bpf_lookup_elem(int fd, void *key, void *value);
11-
int bpf_delete_elem(int fd, void *key);
12-
int bpf_get_next_key(int fd, void *key, void *next_key);
9+
int bpf_map_update_elem(int fd, void *key, void *value, unsigned long long flags);
10+
int bpf_map_lookup_elem(int fd, void *key, void *value);
11+
int bpf_map_delete_elem(int fd, void *key);
12+
int bpf_map_get_next_key(int fd, void *key, void *next_key);
1313

14-
int bpf_prog_load(enum bpf_prog_type prog_type,
15-
const struct bpf_insn *insns, int insn_len,
16-
const char *license, int kern_version);
14+
int bpf_load_program(enum bpf_prog_type prog_type,
15+
const struct bpf_insn *insns, int insn_len,
16+
const char *license, int kern_version,
17+
char *log_buf, size_t log_buf_sz);
1718

1819
int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
1920
int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
2021

2122
int bpf_obj_pin(int fd, const char *pathname);
2223
int bpf_obj_get(const char *pathname);
2324

24-
#define LOG_BUF_SIZE (256 * 1024)
25-
extern char bpf_log_buf[LOG_BUF_SIZE];
25+
#define BPF_LOG_BUF_SIZE (256 * 1024)
2626

2727
/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
2828

samples/bpf/lwt_len_hist_user.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define MAX_INDEX 64
1515
#define MAX_STARS 38
1616

17+
char bpf_log_buf[BPF_LOG_BUF_SIZE];
18+
1719
static void stars(char *str, long val, long max, int width)
1820
{
1921
int i;
@@ -41,13 +43,13 @@ int main(int argc, char **argv)
4143
return -1;
4244
}
4345

44-
while (bpf_get_next_key(map_fd, &key, &next_key) == 0) {
46+
while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
4547
if (next_key >= MAX_INDEX) {
4648
fprintf(stderr, "Key %lu out of bounds\n", next_key);
4749
continue;
4850
}
4951

50-
bpf_lookup_elem(map_fd, &next_key, values);
52+
bpf_map_lookup_elem(map_fd, &next_key, values);
5153

5254
sum = 0;
5355
for (i = 0; i < nr_cpus; i++)

samples/bpf/offwaketime_user.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ static void print_stack(struct key_t *key, __u64 count)
4949
int i;
5050

5151
printf("%s;", key->target);
52-
if (bpf_lookup_elem(map_fd[3], &key->tret, ip) != 0) {
52+
if (bpf_map_lookup_elem(map_fd[3], &key->tret, ip) != 0) {
5353
printf("---;");
5454
} else {
5555
for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
5656
print_ksym(ip[i]);
5757
}
5858
printf("-;");
59-
if (bpf_lookup_elem(map_fd[3], &key->wret, ip) != 0) {
59+
if (bpf_map_lookup_elem(map_fd[3], &key->wret, ip) != 0) {
6060
printf("---;");
6161
} else {
6262
for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
@@ -77,8 +77,8 @@ static void print_stacks(int fd)
7777
struct key_t key = {}, next_key;
7878
__u64 value;
7979

80-
while (bpf_get_next_key(fd, &key, &next_key) == 0) {
81-
bpf_lookup_elem(fd, &next_key, &value);
80+
while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
81+
bpf_map_lookup_elem(fd, &next_key, &value);
8282
print_stack(&next_key, value);
8383
key = next_key;
8484
}

samples/bpf/sampleip_user.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ static void print_ip_map(int fd)
9595

9696
/* fetch IPs and counts */
9797
key = 0, i = 0;
98-
while (bpf_get_next_key(fd, &key, &next_key) == 0) {
99-
bpf_lookup_elem(fd, &next_key, &value);
98+
while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
99+
bpf_map_lookup_elem(fd, &next_key, &value);
100100
counts[i].ip = next_key;
101101
counts[i++].count = value;
102102
key = next_key;

samples/bpf/sock_example.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <stddef.h>
2929
#include "libbpf.h"
3030

31+
char bpf_log_buf[BPF_LOG_BUF_SIZE];
32+
3133
static int test_sock(void)
3234
{
3335
int sock = -1, map_fd, prog_fd, i, key;
@@ -55,8 +57,8 @@ static int test_sock(void)
5557
BPF_EXIT_INSN(),
5658
};
5759

58-
prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, prog, sizeof(prog),
59-
"GPL", 0);
60+
prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, sizeof(prog),
61+
"GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE);
6062
if (prog_fd < 0) {
6163
printf("failed to load prog '%s'\n", strerror(errno));
6264
goto cleanup;
@@ -72,13 +74,13 @@ static int test_sock(void)
7274

7375
for (i = 0; i < 10; i++) {
7476
key = IPPROTO_TCP;
75-
assert(bpf_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
77+
assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
7678

7779
key = IPPROTO_UDP;
78-
assert(bpf_lookup_elem(map_fd, &key, &udp_cnt) == 0);
80+
assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0);
7981

8082
key = IPPROTO_ICMP;
81-
assert(bpf_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
83+
assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
8284

8385
printf("TCP %lld UDP %lld ICMP %lld packets\n",
8486
tcp_cnt, udp_cnt, icmp_cnt);

samples/bpf/sockex1_user.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ int main(int ac, char **argv)
3232
int key;
3333

3434
key = IPPROTO_TCP;
35-
assert(bpf_lookup_elem(map_fd[0], &key, &tcp_cnt) == 0);
35+
assert(bpf_map_lookup_elem(map_fd[0], &key, &tcp_cnt) == 0);
3636

3737
key = IPPROTO_UDP;
38-
assert(bpf_lookup_elem(map_fd[0], &key, &udp_cnt) == 0);
38+
assert(bpf_map_lookup_elem(map_fd[0], &key, &udp_cnt) == 0);
3939

4040
key = IPPROTO_ICMP;
41-
assert(bpf_lookup_elem(map_fd[0], &key, &icmp_cnt) == 0);
41+
assert(bpf_map_lookup_elem(map_fd[0], &key, &icmp_cnt) == 0);
4242

4343
printf("TCP %lld UDP %lld ICMP %lld bytes\n",
4444
tcp_cnt, udp_cnt, icmp_cnt);

samples/bpf/sockex2_user.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ int main(int ac, char **argv)
3939
int key = 0, next_key;
4040
struct pair value;
4141

42-
while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
43-
bpf_lookup_elem(map_fd[0], &next_key, &value);
42+
while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
43+
bpf_map_lookup_elem(map_fd[0], &next_key, &value);
4444
printf("ip %s bytes %lld packets %lld\n",
4545
inet_ntoa((struct in_addr){htonl(next_key)}),
4646
value.bytes, value.packets);

samples/bpf/sockex3_user.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ int main(int argc, char **argv)
5454

5555
sleep(1);
5656
printf("IP src.port -> dst.port bytes packets\n");
57-
while (bpf_get_next_key(map_fd[2], &key, &next_key) == 0) {
58-
bpf_lookup_elem(map_fd[2], &next_key, &value);
57+
while (bpf_map_get_next_key(map_fd[2], &key, &next_key) == 0) {
58+
bpf_map_lookup_elem(map_fd[2], &next_key, &value);
5959
printf("%s.%05d -> %s.%05d %12lld %12lld\n",
6060
inet_ntoa((struct in_addr){htonl(next_key.src)}),
6161
next_key.port16[0],

samples/bpf/spintest_user.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ int main(int ac, char **argv)
3131
for (i = 0; i < 5; i++) {
3232
key = 0;
3333
printf("kprobing funcs:");
34-
while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
35-
bpf_lookup_elem(map_fd[0], &next_key, &value);
34+
while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
35+
bpf_map_lookup_elem(map_fd[0], &next_key, &value);
3636
assert(next_key == value);
3737
sym = ksym_search(value);
3838
printf(" %s", sym->name);
@@ -41,8 +41,8 @@ int main(int ac, char **argv)
4141
if (key)
4242
printf("\n");
4343
key = 0;
44-
while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0)
45-
bpf_delete_elem(map_fd[0], &next_key);
44+
while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0)
45+
bpf_map_delete_elem(map_fd[0], &next_key);
4646
sleep(1);
4747
}
4848

samples/bpf/tc_l2_redirect_user.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ int main(int argc, char **argv)
6060
}
6161

6262
/* bpf_tunnel_key.remote_ipv4 expects host byte orders */
63-
ret = bpf_update_elem(array_fd, &array_key, &ifindex, 0);
63+
ret = bpf_map_update_elem(array_fd, &array_key, &ifindex, 0);
6464
if (ret) {
65-
perror("bpf_update_elem");
65+
perror("bpf_map_update_elem");
6666
goto out;
6767
}
6868

samples/bpf/test_cgrp2_array_pin.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ int main(int argc, char **argv)
8585
}
8686
}
8787

88-
ret = bpf_update_elem(array_fd, &array_key, &cg2_fd, 0);
88+
ret = bpf_map_update_elem(array_fd, &array_key, &cg2_fd, 0);
8989
if (ret) {
90-
perror("bpf_update_elem");
90+
perror("bpf_map_update_elem");
9191
goto out;
9292
}
9393

0 commit comments

Comments
 (0)