Skip to content

Commit 43371c8

Browse files
joestringeracmel
authored andcommitted
samples/bpf: Switch over to libbpf
Now that libbpf under tools/lib/bpf/* is synced with the version from samples/bpf, we can get rid most of the libbpf library here. Committer notes: Built it in a docker fedora rawhide container and ran it in the f25 host, seems to work just like it did before this patch, i.e. the switch to tools/lib/bpf/ doesn't seem to have introduced problems and Joe said he tested it with all the entries in samples/bpf/ and other code he found: [root@f5065a7d6272 linux]# make -j4 O=/tmp/build/linux headers_install <SNIP> [root@f5065a7d6272 linux]# rm -rf /tmp/build/linux/samples/bpf/ [root@f5065a7d6272 linux]# make -j4 O=/tmp/build/linux samples/bpf/ make[1]: Entering directory '/tmp/build/linux' CHK include/config/kernel.release HOSTCC scripts/basic/fixdep GEN ./Makefile CHK include/generated/uapi/linux/version.h Using /git/linux as source for kernel CHK include/generated/utsrelease.h HOSTCC scripts/basic/bin2c HOSTCC arch/x86/tools/relocs_32.o HOSTCC arch/x86/tools/relocs_64.o LD samples/bpf/built-in.o <SNIP> HOSTCC samples/bpf/fds_example.o HOSTCC samples/bpf/sockex1_user.o /git/linux/samples/bpf/fds_example.c: In function 'bpf_prog_create': /git/linux/samples/bpf/fds_example.c:63:6: warning: passing argument 2 of 'bpf_load_program' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers] insns, insns_cnt, "GPL", 0, ^~~~~ In file included from /git/linux/samples/bpf/libbpf.h:5:0, from /git/linux/samples/bpf/bpf_load.h:4, from /git/linux/samples/bpf/fds_example.c:15: /git/linux/tools/lib/bpf/bpf.h:31:5: note: expected 'struct bpf_insn *' but argument is of type 'const struct bpf_insn *' int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, ^~~~~~~~~~~~~~~~ HOSTCC samples/bpf/sockex2_user.o <SNIP> HOSTCC samples/bpf/xdp_tx_iptunnel_user.o clang -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/6.2.1/include -I/git/linux/arch/x86/include -I./arch/x86/include/generated/uapi -I./arch/x86/include/generated -I/git/linux/include -I./include -I/git/linux/arch/x86/include/uapi -I/git/linux/include/uapi -I./include/generated/uapi -include /git/linux/include/linux/kconfig.h \ -D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \ -Wno-compare-distinct-pointer-types \ -Wno-gnu-variable-sized-type-not-at-end \ -Wno-address-of-packed-member -Wno-tautological-compare \ -O2 -emit-llvm -c /git/linux/samples/bpf/sockex1_kern.c -o -| llc -march=bpf -filetype=obj -o samples/bpf/sockex1_kern.o HOSTLD samples/bpf/tc_l2_redirect <SNIP> HOSTLD samples/bpf/lwt_len_hist HOSTLD samples/bpf/xdp_tx_iptunnel make[1]: Leaving directory '/tmp/build/linux' [root@f5065a7d6272 linux]# And then, in the host: [root@jouet bpf]# mount | grep "docker.*devicemapper\/" /dev/mapper/docker-253:0-1705076-9bd8aa1e0af33adce89ff42090847868ca676932878942be53941a06ec5923f9 on /var/lib/docker/devicemapper/mnt/9bd8aa1e0af33adce89ff42090847868ca676932878942be53941a06ec5923f9 type xfs (rw,relatime,context="system_u:object_r:container_file_t:s0:c73,c276",nouuid,attr2,inode64,sunit=1024,swidth=1024,noquota) [root@jouet bpf]# cd /var/lib/docker/devicemapper/mnt/9bd8aa1e0af33adce89ff42090847868ca676932878942be53941a06ec5923f9/rootfs/tmp/build/linux/samples/bpf/ [root@jouet bpf]# 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]=f423d171e0487b2f802b6a792657f0f3c8f6d155, not stripped [root@jouet bpf]# readelf -SW offwaketime offwaketime offwaketime_kern.o offwaketime_user.o [root@jouet bpf]# readelf -SW offwaketime_kern.o There are 11 section headers, starting at offset 0x700: Section Headers: [Nr] Name Type Address Off Size ES Flg Lk Inf Al [ 0] NULL 0000000000000000 000000 000000 00 0 0 0 [ 1] .strtab STRTAB 0000000000000000 000658 0000a8 00 0 0 1 [ 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 [ 4] .relkprobe/try_to_wake_up REL 0000000000000000 0005a8 000020 10 10 3 8 [ 5] tracepoint/sched/sched_switch PROGBITS 0000000000000000 000118 000318 00 AX 0 0 8 [ 6] .reltracepoint/sched/sched_switch REL 0000000000000000 0005c8 000090 10 10 5 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 [10] .symtab SYMTAB 0000000000000000 000488 000120 18 1 4 8 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) [root@jouet bpf]# ./offwaketime | head -3 qemu-system-x86;entry_SYSCALL_64_fastpath;sys_ppoll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;hrtimer_wakeup;__hrtimer_run_queues;hrtimer_interrupt;local_apic_timer_interrupt;smp_apic_timer_interrupt;__irqentry_text_start;cpuidle_enter_state;cpuidle_enter;call_cpuidle;cpu_startup_entry;rest_init;start_kernel;x86_64_start_reservations;x86_64_start_kernel;start_cpu;;swapper/0 4 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 1 swapper/2;start_cpu;start_secondary;cpu_startup_entry;schedule_preempt_disabled;schedule;__schedule;-;---;; 61 [root@jouet bpf]# 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: https://github.com/joestringer/linux/commit/5c40f54a52b1f437123c81e21873f4b4b1f9bd55.patch Link: http://lkml.kernel.org/n/tip-xr8twtx7sjh5821g8qw47yxk@git.kernel.org [ Use -I$(srctree)/tools/lib/ to support out of source code tree builds, as noticed by Wang Nan ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent ed6c166 commit 43371c8

10 files changed

+52
-168
lines changed

samples/bpf/Makefile

+36-32
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,43 @@ hostprogs-y += tc_l2_redirect
3535
hostprogs-y += lwt_len_hist
3636
hostprogs-y += xdp_tx_iptunnel
3737

38-
test_lru_dist-objs := test_lru_dist.o libbpf.o
39-
sock_example-objs := sock_example.o libbpf.o
40-
fds_example-objs := bpf_load.o libbpf.o fds_example.o
41-
sockex1-objs := bpf_load.o libbpf.o sockex1_user.o
42-
sockex2-objs := bpf_load.o libbpf.o sockex2_user.o
43-
sockex3-objs := bpf_load.o libbpf.o sockex3_user.o
44-
tracex1-objs := bpf_load.o libbpf.o tracex1_user.o
45-
tracex2-objs := bpf_load.o libbpf.o tracex2_user.o
46-
tracex3-objs := bpf_load.o libbpf.o tracex3_user.o
47-
tracex4-objs := bpf_load.o libbpf.o tracex4_user.o
48-
tracex5-objs := bpf_load.o libbpf.o tracex5_user.o
49-
tracex6-objs := bpf_load.o libbpf.o tracex6_user.o
50-
test_probe_write_user-objs := bpf_load.o libbpf.o test_probe_write_user_user.o
51-
trace_output-objs := bpf_load.o libbpf.o trace_output_user.o
52-
lathist-objs := bpf_load.o libbpf.o lathist_user.o
53-
offwaketime-objs := bpf_load.o libbpf.o offwaketime_user.o
54-
spintest-objs := bpf_load.o libbpf.o spintest_user.o
55-
map_perf_test-objs := bpf_load.o libbpf.o map_perf_test_user.o
56-
test_overhead-objs := bpf_load.o libbpf.o test_overhead_user.o
57-
test_cgrp2_array_pin-objs := libbpf.o test_cgrp2_array_pin.o
58-
test_cgrp2_attach-objs := libbpf.o test_cgrp2_attach.o
59-
test_cgrp2_attach2-objs := libbpf.o test_cgrp2_attach2.o cgroup_helpers.o
60-
test_cgrp2_sock-objs := libbpf.o test_cgrp2_sock.o
61-
test_cgrp2_sock2-objs := bpf_load.o libbpf.o test_cgrp2_sock2.o
62-
xdp1-objs := bpf_load.o libbpf.o xdp1_user.o
38+
# Libbpf dependencies
39+
LIBBPF := libbpf.o ../../tools/lib/bpf/bpf.o
40+
41+
test_lru_dist-objs := test_lru_dist.o $(LIBBPF)
42+
sock_example-objs := sock_example.o $(LIBBPF)
43+
fds_example-objs := bpf_load.o $(LIBBPF) fds_example.o
44+
sockex1-objs := bpf_load.o $(LIBBPF) sockex1_user.o
45+
sockex2-objs := bpf_load.o $(LIBBPF) sockex2_user.o
46+
sockex3-objs := bpf_load.o $(LIBBPF) sockex3_user.o
47+
tracex1-objs := bpf_load.o $(LIBBPF) tracex1_user.o
48+
tracex2-objs := bpf_load.o $(LIBBPF) tracex2_user.o
49+
tracex3-objs := bpf_load.o $(LIBBPF) tracex3_user.o
50+
tracex4-objs := bpf_load.o $(LIBBPF) tracex4_user.o
51+
tracex5-objs := bpf_load.o $(LIBBPF) tracex5_user.o
52+
tracex6-objs := bpf_load.o $(LIBBPF) tracex6_user.o
53+
test_probe_write_user-objs := bpf_load.o $(LIBBPF) test_probe_write_user_user.o
54+
trace_output-objs := bpf_load.o $(LIBBPF) trace_output_user.o
55+
lathist-objs := bpf_load.o $(LIBBPF) lathist_user.o
56+
offwaketime-objs := bpf_load.o $(LIBBPF) offwaketime_user.o
57+
spintest-objs := bpf_load.o $(LIBBPF) spintest_user.o
58+
map_perf_test-objs := bpf_load.o $(LIBBPF) map_perf_test_user.o
59+
test_overhead-objs := bpf_load.o $(LIBBPF) test_overhead_user.o
60+
test_cgrp2_array_pin-objs := $(LIBBPF) test_cgrp2_array_pin.o
61+
test_cgrp2_attach-objs := $(LIBBPF) test_cgrp2_attach.o
62+
test_cgrp2_attach2-objs := $(LIBBPF) test_cgrp2_attach2.o cgroup_helpers.o
63+
test_cgrp2_sock-objs := $(LIBBPF) test_cgrp2_sock.o
64+
test_cgrp2_sock2-objs := bpf_load.o $(LIBBPF) test_cgrp2_sock2.o
65+
xdp1-objs := bpf_load.o $(LIBBPF) xdp1_user.o
6366
# reuse xdp1 source intentionally
64-
xdp2-objs := bpf_load.o libbpf.o xdp1_user.o
65-
test_current_task_under_cgroup-objs := bpf_load.o libbpf.o cgroup_helpers.o \
67+
xdp2-objs := bpf_load.o $(LIBBPF) xdp1_user.o
68+
test_current_task_under_cgroup-objs := bpf_load.o $(LIBBPF) cgroup_helpers.o \
6669
test_current_task_under_cgroup_user.o
67-
trace_event-objs := bpf_load.o libbpf.o trace_event_user.o
68-
sampleip-objs := bpf_load.o libbpf.o sampleip_user.o
69-
tc_l2_redirect-objs := bpf_load.o libbpf.o tc_l2_redirect_user.o
70-
lwt_len_hist-objs := bpf_load.o libbpf.o lwt_len_hist_user.o
71-
xdp_tx_iptunnel-objs := bpf_load.o libbpf.o xdp_tx_iptunnel_user.o
70+
trace_event-objs := bpf_load.o $(LIBBPF) trace_event_user.o
71+
sampleip-objs := bpf_load.o $(LIBBPF) sampleip_user.o
72+
tc_l2_redirect-objs := bpf_load.o $(LIBBPF) tc_l2_redirect_user.o
73+
lwt_len_hist-objs := bpf_load.o $(LIBBPF) lwt_len_hist_user.o
74+
xdp_tx_iptunnel-objs := bpf_load.o $(LIBBPF) xdp_tx_iptunnel_user.o
7275

7376
# Tell kbuild to always build the programs
7477
always := $(hostprogs-y)
@@ -104,6 +107,7 @@ always += lwt_len_hist_kern.o
104107
always += xdp_tx_iptunnel_kern.o
105108

106109
HOSTCFLAGS += -I$(objtree)/usr/include
110+
HOSTCFLAGS += -I$(srctree)/tools/lib/
107111
HOSTCFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
108112

109113
HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable

samples/bpf/README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
eBPF sample programs
22
====================
33

4-
This directory contains a mini eBPF library, test stubs, verifier
5-
test-suite and examples for using eBPF.
4+
This directory contains a test stubs, verifier test-suite and examples
5+
for using eBPF. The examples use libbpf from tools/lib/bpf.
66

77
Build dependencies
88
==================

samples/bpf/bpf_load.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
6666
bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
6767
bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
6868
bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
69+
size_t insns_cnt = size / sizeof(struct bpf_insn);
6970
enum bpf_prog_type prog_type;
7071
char buf[256];
7172
int fd, efd, err, id;
@@ -95,7 +96,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
9596
return -1;
9697
}
9798

98-
fd = bpf_load_program(prog_type, prog, size, license, kern_version,
99+
fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
99100
bpf_log_buf, BPF_LOG_BUF_SIZE);
100101
if (fd < 0) {
101102
printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);

samples/bpf/fds_example.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ static int bpf_prog_create(const char *object)
5353
BPF_MOV64_IMM(BPF_REG_0, 1),
5454
BPF_EXIT_INSN(),
5555
};
56+
size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
5657

5758
if (object) {
5859
assert(!load_bpf_file((char *)object));
5960
return prog_fd[0];
6061
} else {
6162
return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
62-
insns, sizeof(insns), "GPL", 0,
63+
insns, insns_cnt, "GPL", 0,
6364
bpf_log_buf, BPF_LOG_BUF_SIZE);
6465
}
6566
}

samples/bpf/libbpf.c

-111
Original file line numberDiff line numberDiff line change
@@ -4,105 +4,13 @@
44
#include <linux/unistd.h>
55
#include <unistd.h>
66
#include <string.h>
7-
#include <linux/netlink.h>
8-
#include <linux/bpf.h>
97
#include <errno.h>
108
#include <net/ethernet.h>
119
#include <net/if.h>
1210
#include <linux/if_packet.h>
1311
#include <arpa/inet.h>
1412
#include "libbpf.h"
1513

16-
static __u64 ptr_to_u64(void *ptr)
17-
{
18-
return (__u64) (unsigned long) ptr;
19-
}
20-
21-
int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
22-
int max_entries, int map_flags)
23-
{
24-
union bpf_attr attr = {
25-
.map_type = map_type,
26-
.key_size = key_size,
27-
.value_size = value_size,
28-
.max_entries = max_entries,
29-
.map_flags = map_flags,
30-
};
31-
32-
return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
33-
}
34-
35-
int bpf_map_update_elem(int fd, void *key, void *value, unsigned long long flags)
36-
{
37-
union bpf_attr attr = {
38-
.map_fd = fd,
39-
.key = ptr_to_u64(key),
40-
.value = ptr_to_u64(value),
41-
.flags = flags,
42-
};
43-
44-
return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
45-
}
46-
47-
int bpf_map_lookup_elem(int fd, void *key, void *value)
48-
{
49-
union bpf_attr attr = {
50-
.map_fd = fd,
51-
.key = ptr_to_u64(key),
52-
.value = ptr_to_u64(value),
53-
};
54-
55-
return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
56-
}
57-
58-
int bpf_map_delete_elem(int fd, void *key)
59-
{
60-
union bpf_attr attr = {
61-
.map_fd = fd,
62-
.key = ptr_to_u64(key),
63-
};
64-
65-
return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
66-
}
67-
68-
int bpf_map_get_next_key(int fd, void *key, void *next_key)
69-
{
70-
union bpf_attr attr = {
71-
.map_fd = fd,
72-
.key = ptr_to_u64(key),
73-
.next_key = ptr_to_u64(next_key),
74-
};
75-
76-
return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
77-
}
78-
79-
#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
80-
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)
85-
{
86-
union bpf_attr attr = {
87-
.prog_type = prog_type,
88-
.insns = ptr_to_u64((void *) insns),
89-
.insn_cnt = prog_len / sizeof(struct bpf_insn),
90-
.license = ptr_to_u64((void *) license),
91-
.log_buf = ptr_to_u64(log_buf),
92-
.log_size = log_buf_sz,
93-
.log_level = 1,
94-
};
95-
96-
/* assign one field outside of struct init to make sure any
97-
* padding is zero initialized
98-
*/
99-
attr.kern_version = kern_version;
100-
101-
log_buf[0] = 0;
102-
103-
return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
104-
}
105-
10614
int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
10715
{
10816
union bpf_attr attr = {
@@ -124,25 +32,6 @@ int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
12432
return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr));
12533
}
12634

127-
int bpf_obj_pin(int fd, const char *pathname)
128-
{
129-
union bpf_attr attr = {
130-
.pathname = ptr_to_u64((void *)pathname),
131-
.bpf_fd = fd,
132-
};
133-
134-
return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr));
135-
}
136-
137-
int bpf_obj_get(const char *pathname)
138-
{
139-
union bpf_attr attr = {
140-
.pathname = ptr_to_u64((void *)pathname),
141-
};
142-
143-
return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
144-
}
145-
14635
int open_raw_sock(const char *name)
14736
{
14837
struct sockaddr_ll sll;

samples/bpf/libbpf.h

+2-17
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,13 @@
22
#ifndef __LIBBPF_H
33
#define __LIBBPF_H
44

5-
struct bpf_insn;
6-
7-
int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
8-
int max_entries, int map_flags);
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);
5+
#include <bpf/bpf.h>
136

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);
7+
struct bpf_insn;
188

199
int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
2010
int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
2111

22-
int bpf_obj_pin(int fd, const char *pathname);
23-
int bpf_obj_get(const char *pathname);
24-
25-
#define BPF_LOG_BUF_SIZE (256 * 1024)
26-
2712
/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
2813

2914
#define BPF_ALU64_REG(OP, DST, SRC) \

samples/bpf/sock_example.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ static int test_sock(void)
5656
BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */
5757
BPF_EXIT_INSN(),
5858
};
59+
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
5960

60-
prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, sizeof(prog),
61+
prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, insns_cnt,
6162
"GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE);
6263
if (prog_fd < 0) {
6364
printf("failed to load prog '%s'\n", strerror(errno));

samples/bpf/test_cgrp2_attach.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ static int prog_load(int map_fd, int verdict)
6868
BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
6969
BPF_EXIT_INSN(),
7070
};
71+
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
7172

7273
return bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
73-
prog, sizeof(prog), "GPL", 0,
74+
prog, insns_cnt, "GPL", 0,
7475
bpf_log_buf, BPF_LOG_BUF_SIZE);
7576
}
7677

samples/bpf/test_cgrp2_attach2.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ static int prog_load(int verdict)
4141
BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
4242
BPF_EXIT_INSN(),
4343
};
44+
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
4445

4546
ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
46-
prog, sizeof(prog), "GPL", 0,
47+
prog, insns_cnt, "GPL", 0,
4748
bpf_log_buf, BPF_LOG_BUF_SIZE);
4849

4950
if (ret < 0) {

samples/bpf/test_cgrp2_sock.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ static int prog_load(int idx)
3535
BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = verdict */
3636
BPF_EXIT_INSN(),
3737
};
38+
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
3839

39-
return bpf_load_program(BPF_PROG_TYPE_CGROUP_SOCK, prog, sizeof(prog),
40+
return bpf_load_program(BPF_PROG_TYPE_CGROUP_SOCK, prog, insns_cnt,
4041
"GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE);
4142
}
4243

0 commit comments

Comments
 (0)