History

Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next

Daniel Borkmann says: ==================== pull-request: bpf-next 2019-09-16 The following pull-request contains BPF updates for your *net-next* tree. The main changes are: 1) Now that initial BPF backend for gcc has been merged upstream, enable BPF kselftest suite for bpf-gcc. Also fix a BE issue with access to bpf_sysctl.file_pos, from Ilya. 2) Follow-up fix for link-vmlinux.sh to remove bash-specific extensions related to recent work on exposing BTF info through sysfs, from Andrii. 3) AF_XDP zero copy fixes for i40e and ixgbe driver which caused umem headroom to be added twice, from Ciara. 4) Refactoring work to convert sock opt tests into test_progs framework in BPF kselftests, from Stanislav. 5) Fix a general protection fault in dev_map_hash_update_elem(), from Toke. 6) Cleanup to use BPF_PROG_RUN() macro in KCM, from Sami. ====================

David S. Miller committed 7 years ago

Commit 28f2c362 parents 5f109d4 d895a0f21 files · +260 −322Browse files

Unified · Split

21 files
  1. drivers/net/ethernet/intel/i40e/i40e_xsk.c +2 −2
  2. drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c +2 −2
  3. include/linux/filter.h +4 −4
  4. kernel/bpf/cgroup.c +8 −2
  5. kernel/bpf/devmap.c +12 −5
  6. kernel/bpf/verifier.c +2 −2
  7. net/kcm/kcmsock.c +1 −1
  8. samples/bpf/xdpsock_user.c +1 −1
  9. scripts/link-vmlinux.sh +11 −5
  10. tools/testing/selftests/bpf/.gitignore +0 −5
  11. tools/testing/selftests/bpf/Makefile +53 −24
  12. tools/testing/selftests/bpf/bpf_helpers.h +16 −8
  13. tools/testing/selftests/bpf/prog_tests/sockopt.c +7 −43
  14. tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c +42 −60
  15. tools/testing/selftests/bpf/prog_tests/sockopt_multi.c +10 −52
  16. tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +12 −48
  17. tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +27 −56
  18. tools/testing/selftests/bpf/progs/test_tc_edt.c +1 −0
  19. tools/testing/selftests/bpf/test_progs.c +38 −0
  20. tools/testing/selftests/bpf/test_progs.h +3 −1
  21. tools/testing/selftests/bpf/test_sysctl.c +8 −1
drivers/net/ethernet/intel/i40e/i40e_xsk.c+2−2
@@ -197,9 +197,9 @@ static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp) · 196 unmodified lines
197197 {
198198 struct xdp_umem *umem = rx_ring->xsk_umem;
199199 int err, result = I40E_XDP_PASS;
200− u64 offset = umem->headroom;
201200 struct i40e_ring *xdp_ring;
202201 struct bpf_prog *xdp_prog;
202+ u64 offset;
203203 u32 act;
204204
205205 rcu_read_lock();
@@ -208,7 +208,7 @@ static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp) · 2 unmodified lines
208208 */
209209 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
210210 act = bpf_prog_run_xdp(xdp_prog, xdp);
211− offset += xdp->data - xdp->data_hard_start;
211+ offset = xdp->data - xdp->data_hard_start;
212212
213213 xdp->handle = xsk_umem_adjust_offset(umem, xdp->handle, offset);
214214
drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c+2−2
@@ -145,15 +145,15 @@ static int ixgbe_run_xdp_zc(struct ixgbe_adapter *adapter, · 144 unmodified lines
145145 {
146146 struct xdp_umem *umem = rx_ring->xsk_umem;
147147 int err, result = IXGBE_XDP_PASS;
148− u64 offset = umem->headroom;
149148 struct bpf_prog *xdp_prog;
150149 struct xdp_frame *xdpf;
150+ u64 offset;
151151 u32 act;
152152
153153 rcu_read_lock();
154154 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
155155 act = bpf_prog_run_xdp(xdp_prog, xdp);
156− offset += xdp->data - xdp->data_hard_start;
156+ offset = xdp->data - xdp->data_hard_start;
157157
158158 xdp->handle = xsk_umem_adjust_offset(umem, xdp->handle, offset);
159159
include/linux/filter.h+4−4
@@ -749,14 +749,14 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default) · 748 unmodified lines
749749 }
750750
751751 static inline u8
752−bpf_ctx_narrow_load_shift(u32 off, u32 size, u32 size_default)
752+bpf_ctx_narrow_access_offset(u32 off, u32 size, u32 size_default)
753753 {
754− u8 load_off = off & (size_default - 1);
754+ u8 access_off = off & (size_default - 1);
755755
756756 #ifdef __LITTLE_ENDIAN
757− return load_off * 8;
757+ return access_off;
758758 #else
759− return (size_default - (load_off + size)) * 8;
759+ return size_default - (access_off + size);
760760 #endif
761761 }
762762
kernel/bpf/cgroup.c+8−2
@@ -1334,6 +1334,7 @@ static u32 sysctl_convert_ctx_access(enum bpf_access_type type, · 1333 unmodified lines
13341334 struct bpf_prog *prog, u32 *target_size)
13351335 {
13361336 struct bpf_insn *insn = insn_buf;
1337+ u32 read_size;
13371338
13381339 switch (si->off) {
13391340 case offsetof(struct bpf_sysctl, write):
@@ -1365,7 +1366,9 @@ static u32 sysctl_convert_ctx_access(enum bpf_access_type type, · 25 unmodified lines
13651366 treg, si->dst_reg,
13661367 offsetof(struct bpf_sysctl_kern, ppos));
13671368 *insn++ = BPF_STX_MEM(
1368− BPF_SIZEOF(u32), treg, si->src_reg, 0);
1369+ BPF_SIZEOF(u32), treg, si->src_reg,
1370+ bpf_ctx_narrow_access_offset(
1371+ 0, sizeof(u32), sizeof(loff_t)));
13691372 *insn++ = BPF_LDX_MEM(
13701373 BPF_DW, treg, si->dst_reg,
13711374 offsetof(struct bpf_sysctl_kern, tmp_reg));
@@ -1374,8 +1377,11 @@ static u32 sysctl_convert_ctx_access(enum bpf_access_type type, · 2 unmodified lines
13741377 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
13751378 si->dst_reg, si->src_reg,
13761379 offsetof(struct bpf_sysctl_kern, ppos));
1380+ read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
13771381 *insn++ = BPF_LDX_MEM(
1378− BPF_SIZE(si->code), si->dst_reg, si->dst_reg, 0);
1382+ BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
1383+ bpf_ctx_narrow_access_offset(
1384+ 0, read_size, sizeof(loff_t)));
13791385 }
13801386 *target_size = sizeof(u32);
13811387 break;
kernel/bpf/devmap.c+12−5
@@ -650,19 +650,22 @@ static int __dev_map_hash_update_elem(struct net *net, struct bpf_map *map, · 649 unmodified lines
650650 u32 ifindex = *(u32 *)value;
651651 u32 idx = *(u32 *)key;
652652 unsigned long flags;
653+ int err = -EEXIST;
653654
654655 if (unlikely(map_flags > BPF_EXIST || !ifindex))
655656 return -EINVAL;
656657
658+ spin_lock_irqsave(&dtab->index_lock, flags);
659+
657660 old_dev = __dev_map_hash_lookup_elem(map, idx);
658661 if (old_dev && (map_flags & BPF_NOEXIST))
659− return -EEXIST;
662+ goto out_err;
660663
661664 dev = __dev_map_alloc_node(net, dtab, ifindex, idx);
662− if (IS_ERR(dev))
663− return PTR_ERR(dev);
664−
665− spin_lock_irqsave(&dtab->index_lock, flags);
665+ if (IS_ERR(dev)) {
666+ err = PTR_ERR(dev);
667+ goto out_err;
668+ }
666669
667670 if (old_dev) {
668671 hlist_del_rcu(&old_dev->index_hlist);
@@ -683,6 +686,10 @@ static int __dev_map_hash_update_elem(struct net *net, struct bpf_map *map, · 14 unmodified lines
683686 call_rcu(&old_dev->rcu, __dev_map_entry_free);
684687
685688 return 0;
689+
690+out_err:
691+ spin_unlock_irqrestore(&dtab->index_lock, flags);
692+ return err;
686693 }
687694
688695 static int dev_map_hash_update_elem(struct bpf_map *map, void *key, void *value,
kernel/bpf/verifier.c+2−2
@@ -8624,8 +8624,8 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env) · 8623 unmodified lines
86248624 }
86258625
86268626 if (is_narrower_load && size < target_size) {
8627− u8 shift = bpf_ctx_narrow_load_shift(off, size,
8628− size_default);
8627+ u8 shift = bpf_ctx_narrow_access_offset(
8628+ off, size, size_default) * 8;
86298629 if (ctx_field_size <= 4) {
86308630 if (shift)
86318631 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
net/kcm/kcmsock.c+1−1
@@ -379,7 +379,7 @@ static int kcm_parse_func_strparser(struct strparser *strp, struct sk_buff *skb) · 378 unmodified lines
379379 struct kcm_psock *psock = container_of(strp, struct kcm_psock, strp);
380380 struct bpf_prog *prog = psock->bpf_prog;
381381
382− return (*prog->bpf_func)(skb, prog->insnsi);
382+ return BPF_PROG_RUN(prog, skb);
383383 }
384384
385385 static int kcm_read_sock_done(struct strparser *strp, int err)
samples/bpf/xdpsock_user.c+1−1
@@ -685,7 +685,7 @@ static void l2fwd(struct xsk_socket_info *xsk, struct pollfd *fds) · 684 unmodified lines
685685 for (i = 0; i < rcvd; i++) {
686686 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
687687 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
688− u64 orig = xsk_umem__extract_addr(addr);
688+ u64 orig = addr;
689689
690690 addr = xsk_umem__add_offset_to_addr(addr);
691691 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
scripts/link-vmlinux.sh+11−5
@@ -57,12 +57,16 @@ modpost_link() · 56 unmodified lines
5757
5858 # Link of vmlinux
5959 # ${1} - output file
60−# ${@:2} - optional extra .o files
60+# ${2}, ${3}, ... - optional extra .o files
6161 vmlinux_link()
6262 {
6363 local lds="${objtree}/${KBUILD_LDS}"
64+ local output=${1}
6465 local objects
6566
67+ # skip output file argument
68+ shift
69+
6670 if [ "${SRCARCH}" != "um" ]; then
6771 objects="--whole-archive \
6872 ${KBUILD_VMLINUX_OBJS} \
@@ -70,9 +74,10 @@ vmlinux_link() · 1 unmodified line
7074 --start-group \
7175 ${KBUILD_VMLINUX_LIBS} \
7276 --end-group \
73− ${@:2}"
77+ ${@}"
7478
75− ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${1} \
79+ ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
80+ -o ${output} \
7681 -T ${lds} ${objects}
7782 else
7883 objects="-Wl,--whole-archive \
@@ -81,9 +86,10 @@ vmlinux_link() · 2 unmodified lines
8186 -Wl,--start-group \
8287 ${KBUILD_VMLINUX_LIBS} \
8388 -Wl,--end-group \
84− ${@:2}"
89+ ${@}"
8590
86− ${CC} ${CFLAGS_vmlinux} -o ${1} \
91+ ${CC} ${CFLAGS_vmlinux} \
92+ -o ${output} \
8793 -Wl,-T,${lds} \
8894 ${objects} \
8995 -lutil -lrt -lpthread
tools/testing/selftests/bpf/.gitignore+0−5
@@ -39,8 +39,3 @@ libbpf.so.* · 38 unmodified lines
3939 test_hashmap
4040 test_btf_dump
4141 xdping
42−test_sockopt
43−test_sockopt_sk
44−test_sockopt_multi
45−test_sockopt_inherit
46−test_tcp_rtt
tools/testing/selftests/bpf/Makefile+53−24
@@ -17,6 +17,7 @@ LLC ?= llc · 16 unmodified lines
1717 LLVM_OBJCOPY ?= llvm-objcopy
1818 LLVM_READELF ?= llvm-readelf
1919 BTF_PAHOLE ?= pahole
20+BPF_GCC ?= $(shell command -v bpf-gcc;)
2021 CFLAGS += -g -Wall -O2 -I$(APIDIR) -I$(LIBDIR) -I$(BPFDIR) -I$(GENDIR) $(GENFLAGS) -I../../../include \
2122 -Dbpf_prog_load=bpf_prog_test_load \
2223 -Dbpf_load_program=bpf_test_load_program
@@ -28,8 +29,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test · 5 unmodified lines
2829 test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
2930 test_cgroup_storage test_select_reuseport test_section_names \
3031 test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
31− test_btf_dump test_cgroup_attach xdping test_sockopt test_sockopt_sk \
32− test_sockopt_multi test_sockopt_inherit test_tcp_rtt
32+ test_btf_dump test_cgroup_attach xdping
3333
3434 BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
3535 TEST_GEN_FILES = $(BPF_OBJ_FILES)
@@ -47,6 +47,10 @@ ifneq ($(SUBREG_CODEGEN),) · 11 unmodified lines
4747 TEST_GEN_FILES += $(patsubst %.o,alu32/%.o, $(BPF_OBJ_FILES))
4848 endif
4949
50+ifneq ($(BPF_GCC),)
51+TEST_GEN_FILES += $(patsubst %.o,bpf_gcc/%.o, $(BPF_OBJ_FILES))
52+endif
53+
5054 # Order correspond to 'make run_tests' order
5155 TEST_PROGS := test_kmod.sh \
5256 test_libbpf.sh \
@@ -106,18 +110,13 @@ $(OUTPUT)/test_socket_cookie: cgroup_helpers.c · 53 unmodified lines
106110 $(OUTPUT)/test_sockmap: cgroup_helpers.c
107111 $(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
108112 $(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
109−$(OUTPUT)/test_progs: trace_helpers.c
113+$(OUTPUT)/test_progs: cgroup_helpers.c trace_helpers.c
110114 $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
111115 $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
112116 $(OUTPUT)/test_netcnt: cgroup_helpers.c
113117 $(OUTPUT)/test_sock_fields: cgroup_helpers.c
114118 $(OUTPUT)/test_sysctl: cgroup_helpers.c
115119 $(OUTPUT)/test_cgroup_attach: cgroup_helpers.c
116−$(OUTPUT)/test_sockopt: cgroup_helpers.c
117−$(OUTPUT)/test_sockopt_sk: cgroup_helpers.c
118−$(OUTPUT)/test_sockopt_multi: cgroup_helpers.c
119−$(OUTPUT)/test_sockopt_inherit: cgroup_helpers.c
120−$(OUTPUT)/test_tcp_rtt: cgroup_helpers.c
121120
122121 .PHONY: force
123122
@@ -143,16 +142,19 @@ endif · 19 unmodified lines
143142 #
144143 # Use '-idirafter': Don't interfere with include mechanics except where the
145144 # build would have failed anyways.
146−CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
145+define get_sys_includes
146+$(shell $(1) -v -E - </dev/null 2>&1 \
147147 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
148+endef
149+CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
150+BPF_CFLAGS = -I. -I./include/uapi -I../../../include/uapi \
151+ -I$(OUTPUT)/../usr/include -D__TARGET_ARCH_$(SRCARCH)
148152
149−CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
150− $(CLANG_SYS_INCLUDES) \
151− -Wno-compare-distinct-pointer-types \
152− -D__TARGET_ARCH_$(SRCARCH)
153+CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
154+ -Wno-compare-distinct-pointer-types
153155
154−$(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
155−$(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
156+$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
157+$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
156158
157159 $(OUTPUT)/test_queue_map.o: test_queue_stack_map.h
158160 $(OUTPUT)/test_stack_map.o: test_queue_stack_map.h
@@ -169,12 +171,12 @@ BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ · 10 unmodified lines
169171 /bin/rm -f ./llvm_btf_verify.o)
170172
171173 ifneq ($(BTF_LLVM_PROBE),)
172− CLANG_FLAGS += -g
174+ BPF_CFLAGS += -g
173175 else
174176 ifneq ($(BTF_LLC_PROBE),)
175177 ifneq ($(BTF_PAHOLE_PROBE),)
176178 ifneq ($(BTF_OBJCOPY_PROBE),)
177− CLANG_FLAGS += -g
179+ BPF_CFLAGS += -g
178180 LLC_FLAGS += -mattr=dwarfris
179181 DWARF2BTF = y
180182 endif
@@ -200,7 +202,7 @@ $(ALU32_BUILD_DIR)/test_progs_32: test_progs.c $(OUTPUT)/libbpf.a\ · 19 unmodified lines
200202 | $(ALU32_BUILD_DIR)
201203 $(CC) $(TEST_PROGS_CFLAGS) $(CFLAGS) \
202204 -o $(ALU32_BUILD_DIR)/test_progs_32 \
203− test_progs.c test_stub.c trace_helpers.c prog_tests/*.c \
205+ test_progs.c test_stub.c cgroup_helpers.c trace_helpers.c prog_tests/*.c \
204206 $(OUTPUT)/libbpf.a $(LDLIBS)
205207
206208 $(ALU32_BUILD_DIR)/test_progs_32: $(PROG_TESTS_H)
@@ -208,8 +210,8 @@ $(ALU32_BUILD_DIR)/test_progs_32: prog_tests/*.c · 1 unmodified line
208210
209211 $(ALU32_BUILD_DIR)/%.o: progs/%.c $(ALU32_BUILD_DIR)/test_progs_32 \
210212 | $(ALU32_BUILD_DIR)
211− ($(CLANG) $(CLANG_FLAGS) -O2 -target bpf -emit-llvm -c $< -o - || \
212− echo "clang failed") | \
213+ ($(CLANG) $(BPF_CFLAGS) $(CLANG_CFLAGS) -O2 -target bpf -emit-llvm \
214+ -c $< -o - || echo "clang failed") | \
213215 $(LLC) -march=bpf -mattr=+alu32 -mcpu=$(CPU) $(LLC_FLAGS) \
214216 -filetype=obj -o $@
215217 ifeq ($(DWARF2BTF),y)
@@ -217,10 +219,37 @@ ifeq ($(DWARF2BTF),y) · 1 unmodified line
217219 endif
218220 endif
219221
222+ifneq ($(BPF_GCC),)
223+GCC_SYS_INCLUDES = $(call get_sys_includes,gcc)
224+IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
225+ grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
226+ifeq ($(IS_LITTLE_ENDIAN),)
227+MENDIAN=-mbig-endian
228+else
229+MENDIAN=-mlittle-endian
230+endif
231+BPF_GCC_CFLAGS = $(GCC_SYS_INCLUDES) $(MENDIAN)
232+BPF_GCC_BUILD_DIR = $(OUTPUT)/bpf_gcc
233+TEST_CUSTOM_PROGS += $(BPF_GCC_BUILD_DIR)/test_progs_bpf_gcc
234+$(BPF_GCC_BUILD_DIR):
235+ mkdir -p $@
236+
237+$(BPF_GCC_BUILD_DIR)/urandom_read: $(OUTPUT)/urandom_read | $(BPF_GCC_BUILD_DIR)
238+ cp $< $@
239+
240+$(BPF_GCC_BUILD_DIR)/test_progs_bpf_gcc: $(OUTPUT)/test_progs \
241+ | $(BPF_GCC_BUILD_DIR)
242+ cp $< $@
243+
244+$(BPF_GCC_BUILD_DIR)/%.o: progs/%.c $(BPF_GCC_BUILD_DIR)/test_progs_bpf_gcc \
245+ | $(BPF_GCC_BUILD_DIR)
246+ $(BPF_GCC) $(BPF_CFLAGS) $(BPF_GCC_CFLAGS) -O2 -c $< -o $@
247+endif
248+
220249 # Have one program compiled without "-target bpf" to test whether libbpf loads
221250 # it successfully
222251 $(OUTPUT)/test_xdp.o: progs/test_xdp.c
223− ($(CLANG) $(CLANG_FLAGS) -O2 -emit-llvm -c $< -o - || \
252+ ($(CLANG) $(BPF_CFLAGS) $(CLANG_CFLAGS) -O2 -emit-llvm -c $< -o - || \
224253 echo "clang failed") | \
225254 $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
226255 ifeq ($(DWARF2BTF),y)
@@ -228,8 +257,8 @@ ifeq ($(DWARF2BTF),y) · 1 unmodified line
228257 endif
229258
230259 $(OUTPUT)/%.o: progs/%.c
231− ($(CLANG) $(CLANG_FLAGS) -O2 -target bpf -emit-llvm -c $< -o - || \
232− echo "clang failed") | \
260+ ($(CLANG) $(BPF_CFLAGS) $(CLANG_CFLAGS) -O2 -target bpf -emit-llvm \
261+ -c $< -o - || echo "clang failed") | \
233262 $(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
234263 ifeq ($(DWARF2BTF),y)
235264 $(BTF_PAHOLE) -J $@
@@ -288,6 +317,6 @@ $(VERIFIER_TESTS_H): $(VERIFIER_TEST_FILES) | $(VERIFIER_TESTS_DIR) · 52 unmodified lines
288317 echo '#endif' \
289318 ) > $(VERIFIER_TESTS_H))
290319
291−EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(ALU32_BUILD_DIR) \
320+EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(ALU32_BUILD_DIR) $(BPF_GCC_BUILD_DIR) \
292321 $(VERIFIER_TESTS_H) $(PROG_TESTS_H) $(MAP_TESTS_H) \
293322 feature
tools/testing/selftests/bpf/bpf_helpers.h+16−8
@@ -1,12 +1,6 @@
11 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2−#ifndef __BPF_HELPERS_H
3−#define __BPF_HELPERS_H
4−
5−/* helper macro to place programs, maps, license in
6− * different sections in elf_bpf file. Section names
7− * are interpreted by elf_bpf loader
8− */
9−#define SEC(NAME) __attribute__((section(NAME), used))
2+#ifndef __BPF_HELPERS__
3+#define __BPF_HELPERS__
104
115 #define __uint(name, val) int (*name)[val]
126 #define __type(name, val) val *name
@@ -19,6 +13,14 @@ · 6 unmodified lines
1913 ##__VA_ARGS__); \
2014 })
2115
16+#ifdef __clang__
17+
18+/* helper macro to place programs, maps, license in
19+ * different sections in elf_bpf file. Section names
20+ * are interpreted by elf_bpf loader
21+ */
22+#define SEC(NAME) __attribute__((section(NAME), used))
23+
2224 /* helper functions called from eBPF programs written in C */
2325 static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
2426 (void *) BPF_FUNC_map_lookup_elem;
@@ -256,6 +258,12 @@ struct bpf_map_def { · 231 unmodified lines
256258 unsigned int numa_node;
257259 };
258260
261+#else
262+
263+#include <bpf-helpers.h>
264+
265+#endif
266+
259267 #define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
260268 struct ____btf_map_##name { \
261269 type_key key; \
tools/testing/selftests/bpf/prog_tests/sockopt.crenamed from tools/testing/selftests/bpf/test_sockopt.c+7−43
@@ -1,22 +1,7 @@
11 // SPDX-License-Identifier: GPL-2.0
2−
3−#include <errno.h>
4−#include <stdio.h>
5−#include <unistd.h>
6−#include <sys/types.h>
7−#include <sys/socket.h>
8−#include <netinet/in.h>
9−
10−#include <linux/filter.h>
11−#include <bpf/bpf.h>
12−#include <bpf/libbpf.h>
13−
14−#include "bpf_rlimit.h"
15−#include "bpf_util.h"
2+#include <test_progs.h>
163 #include "cgroup_helpers.h"
174
18−#define CG_PATH "/sockopt"
19−
205 static char bpf_log_buf[4096];
216 static bool verbose;
227
@@ -983,39 +968,18 @@ close_prog_fd: · 960 unmodified lines
983968 return ret;
984969 }
985970
986−int main(int args, char **argv)
971+void test_sockopt(void)
987972 {
988− int err = EXIT_FAILURE, error_cnt = 0;
989973 int cgroup_fd, i;
990974
991− if (setup_cgroup_environment())
992− goto cleanup_obj;
993−
994− cgroup_fd = create_and_get_cgroup(CG_PATH);
995− if (cgroup_fd < 0)
996− goto cleanup_cgroup_env;
997−
998− if (join_cgroup(CG_PATH))
999− goto cleanup_cgroup;
975+ cgroup_fd = test__join_cgroup("/sockopt");
976+ if (CHECK_FAIL(cgroup_fd < 0))
977+ return;
1000978
1001979 for (i = 0; i < ARRAY_SIZE(tests); i++) {
1002− int err = run_test(cgroup_fd, &tests[i]);
1003−
1004− if (err)
1005− error_cnt++;
1006−
1007− printf("#%d %s: %s\n", i, err ? "FAIL" : "PASS",
1008− tests[i].descr);
980+ test__start_subtest(tests[i].descr);
981+ CHECK_FAIL(run_test(cgroup_fd, &tests[i]));
1009982 }
1010983
1011− printf("Summary: %ld PASSED, %d FAILED\n",
1012− ARRAY_SIZE(tests) - error_cnt, error_cnt);
1013− err = error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
1014−
1015−cleanup_cgroup:
1016984 close(cgroup_fd);
1017−cleanup_cgroup_env:
1018− cleanup_cgroup_environment();
1019−cleanup_obj:
1020− return err;
1021985 }
tools/testing/selftests/bpf/prog_tests/sockopt_inherit.crenamed from tools/testing/selftests/bpf/test_sockopt_inherit.c+42−60
@@ -1,22 +1,7 @@
11 // SPDX-License-Identifier: GPL-2.0
2−#include <error.h>
3−#include <errno.h>
4−#include <stdio.h>
5−#include <unistd.h>
6−#include <sys/types.h>
7−#include <sys/socket.h>
8−#include <netinet/in.h>
9−#include <pthread.h>
10−
11−#include <linux/filter.h>
12−#include <bpf/bpf.h>
13−#include <bpf/libbpf.h>
14−
15−#include "bpf_rlimit.h"
16−#include "bpf_util.h"
2+#include <test_progs.h>
173 #include "cgroup_helpers.h"
184
19−#define CG_PATH "/sockopt_inherit"
205 #define SOL_CUSTOM 0xdeadbeef
216 #define CUSTOM_INHERIT1 0
227 #define CUSTOM_INHERIT2 1
@@ -74,6 +59,9 @@ static int verify_sockopt(int fd, int optname, const char *msg, char expected) · 51 unmodified lines
7459 return 0;
7560 }
7661
62+static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
63+static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
64+
7765 static void *server_thread(void *arg)
7866 {
7967 struct sockaddr_storage addr;
@@ -82,16 +70,26 @@ static void *server_thread(void *arg) · 2 unmodified lines
8270 int client_fd;
8371 int err = 0;
8472
85− if (listen(fd, 1) < 0)
86− error(1, errno, "Failed to listed on socket");
73+ err = listen(fd, 1);
74+
75+ pthread_mutex_lock(&server_started_mtx);
76+ pthread_cond_signal(&server_started);
77+ pthread_mutex_unlock(&server_started_mtx);
78+
79+ if (CHECK_FAIL(err < 0)) {
80+ perror("Failed to listed on socket");
81+ return NULL;
82+ }
8783
8884 err += verify_sockopt(fd, CUSTOM_INHERIT1, "listen", 1);
8985 err += verify_sockopt(fd, CUSTOM_INHERIT2, "listen", 1);
9086 err += verify_sockopt(fd, CUSTOM_LISTENER, "listen", 1);
9187
9288 client_fd = accept(fd, (struct sockaddr *)&addr, &len);
93− if (client_fd < 0)
94− error(1, errno, "Failed to accept client");
89+ if (CHECK_FAIL(client_fd < 0)) {
90+ perror("Failed to accept client");
91+ return NULL;
92+ }
9593
9694 err += verify_sockopt(client_fd, CUSTOM_INHERIT1, "accept", 1);
9795 err += verify_sockopt(client_fd, CUSTOM_INHERIT2, "accept", 1);
@@ -167,7 +165,7 @@ static int prog_attach(struct bpf_object *obj, int cgroup_fd, const char *title) · 69 unmodified lines
167165 return 0;
168166 }
169167
170−static int run_test(int cgroup_fd)
168+static void run_test(int cgroup_fd)
171169 {
172170 struct bpf_prog_load_attr attr = {
173171 .file = "./sockopt_inherit.o",
@@ -180,40 +178,41 @@ static int run_test(int cgroup_fd) · 6 unmodified lines
180178 int err;
181179
182180 err = bpf_prog_load_xattr(&attr, &obj, &ignored);
183− if (err) {
184− log_err("Failed to load BPF object");
185− return -1;
186− }
181+ if (CHECK_FAIL(err))
182+ return;
187183
188184 err = prog_attach(obj, cgroup_fd, "cgroup/getsockopt");
189− if (err)
185+ if (CHECK_FAIL(err))
190186 goto close_bpf_object;
191187
192188 err = prog_attach(obj, cgroup_fd, "cgroup/setsockopt");
193− if (err)
189+ if (CHECK_FAIL(err))
194190 goto close_bpf_object;
195191
196192 server_fd = start_server();
197− if (server_fd < 0) {
198− err = -1;
193+ if (CHECK_FAIL(server_fd < 0))
194+ goto close_bpf_object;
195+
196+ if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
197+ (void *)&server_fd)))
199198 goto close_bpf_object;
200− }
201199
202− pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
200+ pthread_mutex_lock(&server_started_mtx);
201+ pthread_cond_wait(&server_started, &server_started_mtx);
202+ pthread_mutex_unlock(&server_started_mtx);
203203
204204 client_fd = connect_to_server(server_fd);
205− if (client_fd < 0) {
206− err = -1;
205+ if (CHECK_FAIL(client_fd < 0))
207206 goto close_server_fd;
208− }
209207
210− err += verify_sockopt(client_fd, CUSTOM_INHERIT1, "connect", 0);
211− err += verify_sockopt(client_fd, CUSTOM_INHERIT2, "connect", 0);
212− err += verify_sockopt(client_fd, CUSTOM_LISTENER, "connect", 0);
208+ CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_INHERIT1, "connect", 0));
209+ CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_INHERIT2, "connect", 0));
210+ CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_LISTENER, "connect", 0));
213211
214212 pthread_join(tid, &server_err);
215213
216− err += (int)(long)server_err;
214+ err = (int)(long)server_err;
215+ CHECK_FAIL(err);
217216
218217 close(client_fd);
219218
@@ -221,33 +220,16 @@ close_server_fd: · 1 unmodified line
221220 close(server_fd);
222221 close_bpf_object:
223222 bpf_object__close(obj);
224− return err;
225223 }
226224
227−int main(int args, char **argv)
225+void test_sockopt_inherit(void)
228226 {
229227 int cgroup_fd;
230− int err = EXIT_SUCCESS;
231−
232− if (setup_cgroup_environment())
233− return err;
234−
235− cgroup_fd = create_and_get_cgroup(CG_PATH);
236− if (cgroup_fd < 0)
237− goto cleanup_cgroup_env;
238−
239− if (join_cgroup(CG_PATH))
240− goto cleanup_cgroup;
241−
242− if (run_test(cgroup_fd))
243− err = EXIT_FAILURE;
244228
245− printf("test_sockopt_inherit: %s\n",
246− err == EXIT_SUCCESS ? "PASSED" : "FAILED");
229+ cgroup_fd = test__join_cgroup("/sockopt_inherit");
230+ if (CHECK_FAIL(cgroup_fd < 0))
231+ return;
247232
248−cleanup_cgroup:
233+ run_test(cgroup_fd);
249234 close(cgroup_fd);
250−cleanup_cgroup_env:
251− cleanup_cgroup_environment();
252− return err;
253235 }
tools/testing/selftests/bpf/prog_tests/sockopt_multi.crenamed from tools/testing/selftests/bpf/test_sockopt_multi.c+10−52
@@ -1,19 +1,5 @@
11 // SPDX-License-Identifier: GPL-2.0
2−
3−#include <error.h>
4−#include <errno.h>
5−#include <stdio.h>
6−#include <unistd.h>
7−#include <sys/types.h>
8−#include <sys/socket.h>
9−#include <netinet/in.h>
10−
11−#include <linux/filter.h>
12−#include <bpf/bpf.h>
13−#include <bpf/libbpf.h>
14−
15−#include "bpf_rlimit.h"
16−#include "bpf_util.h"
2+#include <test_progs.h>
173 #include "cgroup_helpers.h"
184
195 static int prog_attach(struct bpf_object *obj, int cgroup_fd, const char *title)
@@ -308,7 +294,7 @@ detach: · 288 unmodified lines
308294 return err;
309295 }
310296
311−int main(int argc, char **argv)
297+void test_sockopt_multi(void)
312298 {
313299 struct bpf_prog_load_attr attr = {
314300 .file = "./sockopt_multi.o",
@@ -319,56 +305,28 @@ int main(int argc, char **argv) · 4 unmodified lines
319305 int err = -1;
320306 int ignored;
321307
322− if (setup_cgroup_environment()) {
323− log_err("Failed to setup cgroup environment\n");
324− goto out;
325− }
326−
327− cg_parent = create_and_get_cgroup("/parent");
328− if (cg_parent < 0) {
329− log_err("Failed to create cgroup /parent\n");
330− goto out;
331− }
332−
333− cg_child = create_and_get_cgroup("/parent/child");
334− if (cg_child < 0) {
335− log_err("Failed to create cgroup /parent/child\n");
308+ cg_parent = test__join_cgroup("/parent");
309+ if (CHECK_FAIL(cg_parent < 0))
336310 goto out;
337− }
338311
339− if (join_cgroup("/parent/child")) {
340− log_err("Failed to join cgroup /parent/child\n");
312+ cg_child = test__join_cgroup("/parent/child");
313+ if (CHECK_FAIL(cg_child < 0))
341314 goto out;
342− }
343315
344316 err = bpf_prog_load_xattr(&attr, &obj, &ignored);
345− if (err) {
346− log_err("Failed to load BPF object");
317+ if (CHECK_FAIL(err))
347318 goto out;
348− }
349319
350320 sock_fd = socket(AF_INET, SOCK_STREAM, 0);
351− if (sock_fd < 0) {
352− log_err("Failed to create socket");
321+ if (CHECK_FAIL(sock_fd < 0))
353322 goto out;
354− }
355323
356− if (run_getsockopt_test(obj, cg_parent, cg_child, sock_fd))
357− err = -1;
358− printf("test_sockopt_multi: getsockopt %s\n",
359− err ? "FAILED" : "PASSED");
360−
361− if (run_setsockopt_test(obj, cg_parent, cg_child, sock_fd))
362− err = -1;
363− printf("test_sockopt_multi: setsockopt %s\n",
364− err ? "FAILED" : "PASSED");
324+ CHECK_FAIL(run_getsockopt_test(obj, cg_parent, cg_child, sock_fd));
325+ CHECK_FAIL(run_setsockopt_test(obj, cg_parent, cg_child, sock_fd));
365326
366327 out:
367328 close(sock_fd);
368329 bpf_object__close(obj);
369330 close(cg_child);
370331 close(cg_parent);
371−
372− printf("test_sockopt_multi: %s\n", err ? "FAILED" : "PASSED");
373− return err ? EXIT_FAILURE : EXIT_SUCCESS;
374332 }
tools/testing/selftests/bpf/prog_tests/sockopt_sk.crenamed from tools/testing/selftests/bpf/test_sockopt_sk.c+12−48
@@ -1,23 +1,7 @@
11 // SPDX-License-Identifier: GPL-2.0
2−
3−#include <errno.h>
4−#include <stdio.h>
5−#include <unistd.h>
6−#include <sys/types.h>
7−#include <sys/socket.h>
8−#include <netinet/in.h>
9−#include <netinet/tcp.h>
10−
11−#include <linux/filter.h>
12−#include <bpf/bpf.h>
13−#include <bpf/libbpf.h>
14−
15−#include "bpf_rlimit.h"
16−#include "bpf_util.h"
2+#include <test_progs.h>
173 #include "cgroup_helpers.h"
184
19−#define CG_PATH "/sockopt"
20−
215 #define SOL_CUSTOM 0xdeadbeef
226
237 static int getsetsockopt(void)
@@ -176,7 +160,7 @@ static int prog_attach(struct bpf_object *obj, int cgroup_fd, const char *title) · 152 unmodified lines
176160 return 0;
177161 }
178162
179−static int run_test(int cgroup_fd)
163+static void run_test(int cgroup_fd)
180164 {
181165 struct bpf_prog_load_attr attr = {
182166 .file = "./sockopt_sk.o",
@@ -186,51 +170,31 @@ static int run_test(int cgroup_fd) · 3 unmodified lines
186170 int err;
187171
188172 err = bpf_prog_load_xattr(&attr, &obj, &ignored);
189− if (err) {
190− log_err("Failed to load BPF object");
191− return -1;
192− }
173+ if (CHECK_FAIL(err))
174+ return;
193175
194176 err = prog_attach(obj, cgroup_fd, "cgroup/getsockopt");
195− if (err)
177+ if (CHECK_FAIL(err))
196178 goto close_bpf_object;
197179
198180 err = prog_attach(obj, cgroup_fd, "cgroup/setsockopt");
199− if (err)
181+ if (CHECK_FAIL(err))
200182 goto close_bpf_object;
201183
202− err = getsetsockopt();
184+ CHECK_FAIL(getsetsockopt());
203185
204186 close_bpf_object:
205187 bpf_object__close(obj);
206− return err;
207188 }
208189
209−int main(int args, char **argv)
190+void test_sockopt_sk(void)
210191 {
211192 int cgroup_fd;
212− int err = EXIT_SUCCESS;
213−
214− if (setup_cgroup_environment())
215− goto cleanup_obj;
216−
217− cgroup_fd = create_and_get_cgroup(CG_PATH);
218− if (cgroup_fd < 0)
219− goto cleanup_cgroup_env;
220−
221− if (join_cgroup(CG_PATH))
222− goto cleanup_cgroup;
223−
224− if (run_test(cgroup_fd))
225− err = EXIT_FAILURE;
226193
227− printf("test_sockopt_sk: %s\n",
228− err == EXIT_SUCCESS ? "PASSED" : "FAILED");
194+ cgroup_fd = test__join_cgroup("/sockopt_sk");
195+ if (CHECK_FAIL(cgroup_fd < 0))
196+ return;
229197
230−cleanup_cgroup:
198+ run_test(cgroup_fd);
231199 close(cgroup_fd);
232−cleanup_cgroup_env:
233− cleanup_cgroup_environment();
234−cleanup_obj:
235− return err;
236200 }
tools/testing/selftests/bpf/prog_tests/tcp_rtt.crenamed from tools/testing/selftests/bpf/test_tcp_rtt.c+27−56
@@ -1,24 +1,7 @@
11 // SPDX-License-Identifier: GPL-2.0
2−#include <error.h>
3−#include <errno.h>
4−#include <stdio.h>
5−#include <unistd.h>
6−#include <sys/types.h>
7−#include <sys/socket.h>
8−#include <netinet/in.h>
9−#include <netinet/tcp.h>
10−#include <pthread.h>
11−
12−#include <linux/filter.h>
13−#include <bpf/bpf.h>
14−#include <bpf/libbpf.h>
15−
16−#include "bpf_rlimit.h"
17−#include "bpf_util.h"
2+#include <test_progs.h>
183 #include "cgroup_helpers.h"
194
20−#define CG_PATH "/tcp_rtt"
21−
225 struct tcp_rtt_storage {
236 __u32 invoked;
247 __u32 dsack_dups;
@@ -31,8 +14,8 @@ static void send_byte(int fd) · 6 unmodified lines
3114 {
3215 char b = 0x55;
3316
34− if (write(fd, &b, sizeof(b)) != 1)
35− error(1, errno, "Failed to send single byte");
17+ if (CHECK_FAIL(write(fd, &b, sizeof(b)) != 1))
18+ perror("Failed to send single byte");
3619 }
3720
3821 static int wait_for_ack(int fd, int retries)
@@ -66,8 +49,10 @@ static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 invoked, · 27 unmodified lines
6649 int err = 0;
6750 struct tcp_rtt_storage val;
6851
69− if (bpf_map_lookup_elem(map_fd, &client_fd, &val) < 0)
70− error(1, errno, "Failed to read socket storage");
52+ if (CHECK_FAIL(bpf_map_lookup_elem(map_fd, &client_fd, &val) < 0)) {
53+ perror("Failed to read socket storage");
54+ return -1;
55+ }
7156
7257 if (val.invoked != invoked) {
7358 log_err("%s: unexpected bpf_tcp_sock.invoked %d != %d",
@@ -225,61 +210,47 @@ static void *server_thread(void *arg) · 151 unmodified lines
225210 int fd = *(int *)arg;
226211 int client_fd;
227212
228− if (listen(fd, 1) < 0)
229− error(1, errno, "Failed to listed on socket");
213+ if (CHECK_FAIL(listen(fd, 1)) < 0) {
214+ perror("Failed to listed on socket");
215+ return NULL;
216+ }
230217
231218 client_fd = accept(fd, (struct sockaddr *)&addr, &len);
232− if (client_fd < 0)
233− error(1, errno, "Failed to accept client");
219+ if (CHECK_FAIL(client_fd < 0)) {
220+ perror("Failed to accept client");
221+ return NULL;
222+ }
234223
235224 /* Wait for the next connection (that never arrives)
236225 * to keep this thread alive to prevent calling
237226 * close() on client_fd.
238227 */
239− if (accept(fd, (struct sockaddr *)&addr, &len) >= 0)
240− error(1, errno, "Unexpected success in second accept");
228+ if (CHECK_FAIL(accept(fd, (struct sockaddr *)&addr, &len) >= 0)) {
229+ perror("Unexpected success in second accept");
230+ return NULL;
231+ }
241232
242233 close(client_fd);
243234
244235 return NULL;
245236 }
246237
247−int main(int args, char **argv)
238+void test_tcp_rtt(void)
248239 {
249240 int server_fd, cgroup_fd;
250− int err = EXIT_SUCCESS;
251241 pthread_t tid;
252242
253− if (setup_cgroup_environment())
254− goto cleanup_obj;
255−
256− cgroup_fd = create_and_get_cgroup(CG_PATH);
257− if (cgroup_fd < 0)
258− goto cleanup_cgroup_env;
259−
260− if (join_cgroup(CG_PATH))
261− goto cleanup_cgroup;
243+ cgroup_fd = test__join_cgroup("/tcp_rtt");
244+ if (CHECK_FAIL(cgroup_fd < 0))
245+ return;
262246
263247 server_fd = start_server();
264− if (server_fd < 0) {
265− err = EXIT_FAILURE;
266− goto cleanup_cgroup;
267− }
248+ if (CHECK_FAIL(server_fd < 0))
249+ goto close_cgroup_fd;
268250
269251 pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
270−
271− if (run_test(cgroup_fd, server_fd))
272− err = EXIT_FAILURE;
273−
252+ CHECK_FAIL(run_test(cgroup_fd, server_fd));
274253 close(server_fd);
275−
276− printf("test_sockopt_sk: %s\n",
277− err == EXIT_SUCCESS ? "PASSED" : "FAILED");
278−
279−cleanup_cgroup:
254+close_cgroup_fd:
280255 close(cgroup_fd);
281−cleanup_cgroup_env:
282− cleanup_cgroup_environment();
283−cleanup_obj:
284− return err;
285256 }
tools/testing/selftests/bpf/progs/test_tc_edt.c+1−0
@@ -2,6 +2,7 @@ · 1 unmodified line
22 #include <stdint.h>
33 #include <linux/bpf.h>
44 #include <linux/if_ether.h>
5+#include <linux/stddef.h>
56 #include <linux/in.h>
67 #include <linux/ip.h>
78 #include <linux/pkt_cls.h>
tools/testing/selftests/bpf/test_progs.c+38−0
@@ -2,6 +2,7 @@ · 1 unmodified line
22 /* Copyright (c) 2017 Facebook
33 */
44 #include "test_progs.h"
5+#include "cgroup_helpers.h"
56 #include "bpf_rlimit.h"
67 #include <argp.h>
78 #include <string.h>
@@ -17,6 +18,7 @@ struct prog_test_def { · 9 unmodified lines
1718 int error_cnt;
1819 int skip_cnt;
1920 bool tested;
21+ bool need_cgroup_cleanup;
2022
2123 const char *subtest_name;
2224 int subtest_num;
@@ -122,6 +124,39 @@ void test__fail(void) · 99 unmodified lines
122124 env.test->error_cnt++;
123125 }
124126
127+int test__join_cgroup(const char *path)
128+{
129+ int fd;
130+
131+ if (!env.test->need_cgroup_cleanup) {
132+ if (setup_cgroup_environment()) {
133+ fprintf(stderr,
134+ "#%d %s: Failed to setup cgroup environment\n",
135+ env.test->test_num, env.test->test_name);
136+ return -1;
137+ }
138+
139+ env.test->need_cgroup_cleanup = true;
140+ }
141+
142+ fd = create_and_get_cgroup(path);
143+ if (fd < 0) {
144+ fprintf(stderr,
145+ "#%d %s: Failed to create cgroup '%s' (errno=%d)\n",
146+ env.test->test_num, env.test->test_name, path, errno);
147+ return fd;
148+ }
149+
150+ if (join_cgroup(path)) {
151+ fprintf(stderr,
152+ "#%d %s: Failed to join cgroup '%s' (errno=%d)\n",
153+ env.test->test_num, env.test->test_name, path, errno);
154+ return -1;
155+ }
156+
157+ return fd;
158+}
159+
125160 struct ipv4_packet pkt_v4 = {
126161 .eth.h_proto = __bpf_constant_htons(ETH_P_IP),
127162 .iph.ihl = 5,
@@ -530,6 +565,9 @@ int main(int argc, char **argv) · 402 unmodified lines
530565 fprintf(env.stdout, "#%d %s:%s\n",
531566 test->test_num, test->test_name,
532567 test->error_cnt ? "FAIL" : "OK");
568+
569+ if (test->need_cgroup_cleanup)
570+ cleanup_cgroup_environment();
533571 }
534572 stdio_restore();
535573 printf("Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
tools/testing/selftests/bpf/test_progs.h+3−1
@@ -16,9 +16,10 @@ typedef __u16 __sum16; · 15 unmodified lines
1616 #include <linux/if_packet.h>
1717 #include <linux/ip.h>
1818 #include <linux/ipv6.h>
19−#include <linux/tcp.h>
19+#include <netinet/tcp.h>
2020 #include <linux/filter.h>
2121 #include <linux/perf_event.h>
22+#include <linux/socket.h>
2223 #include <linux/unistd.h>
2324
2425 #include <sys/ioctl.h>
@@ -71,6 +72,7 @@ extern void test__force_log(); · 46 unmodified lines
7172 extern bool test__start_subtest(const char *name);
7273 extern void test__skip(void);
7374 extern void test__fail(void);
75+extern int test__join_cgroup(const char *path);
7476
7577 #define MAGIC_BYTES 123
7678
tools/testing/selftests/bpf/test_sysctl.c+8−1
@@ -32,6 +32,7 @@ struct sysctl_test { · 31 unmodified lines
3232 enum bpf_attach_type attach_type;
3333 const char *sysctl;
3434 int open_flags;
35+ int seek;
3536 const char *newval;
3637 const char *oldval;
3738 enum {
@@ -140,7 +141,7 @@ static struct sysctl_test tests[] = { · 102 unmodified lines
140141 /* If (file_pos == X) */
141142 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
142143 offsetof(struct bpf_sysctl, file_pos)),
143− BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0, 2),
144+ BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 3, 2),
144145
145146 /* return ALLOW; */
146147 BPF_MOV64_IMM(BPF_REG_0, 1),
@@ -153,6 +154,7 @@ static struct sysctl_test tests[] = { · 6 unmodified lines
153154 .attach_type = BPF_CGROUP_SYSCTL,
154155 .sysctl = "kernel/ostype",
155156 .open_flags = O_RDONLY,
157+ .seek = 3,
156158 .result = SUCCESS,
157159 },
158160 {
@@ -1481,6 +1483,11 @@ static int access_sysctl(const char *sysctl_path, · 1322 unmodified lines
14811483 if (fd < 0)
14821484 return fd;
14831485
1486+ if (test->seek && lseek(fd, test->seek, SEEK_SET) == -1) {
1487+ log_err("lseek(%d) failed", test->seek);
1488+ goto err;
1489+ }
1490+
14841491 if (test->open_flags == O_RDONLY) {
14851492 char buf[128];
14861493

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t