60ecebf50a1329035e2b56806175f60d4330e1d2
[openwrt/staging/blocktrron.git] /
1 From e333013ee167444adefd8a292e401b70e97dd4b2 Mon Sep 17 00:00:00 2001
2 From: "Jason A. Donenfeld" <Jason@zx2c4.com>
3 Date: Sun, 15 Dec 2019 22:08:00 +0100
4 Subject: [PATCH 074/124] wireguard: selftests: import harness makefile for
5 test suite
6
7 commit 65d88d04114bca7d85faebd5fed61069cb2b632c upstream.
8
9 WireGuard has been using this on build.wireguard.com for the last
10 several years with considerable success. It allows for very quick and
11 iterative development cycles, and supports several platforms.
12
13 To run the test suite on your current platform in QEMU:
14
15 $ make -C tools/testing/selftests/wireguard/qemu -j$(nproc)
16
17 To run it with KASAN and such turned on:
18
19 $ DEBUG_KERNEL=yes make -C tools/testing/selftests/wireguard/qemu -j$(nproc)
20
21 To run it emulated for another platform in QEMU:
22
23 $ ARCH=arm make -C tools/testing/selftests/wireguard/qemu -j$(nproc)
24
25 At the moment, we support aarch64_be, aarch64, arm, armeb, i686, m68k,
26 mips64, mips64el, mips, mipsel, powerpc64le, powerpc, and x86_64.
27
28 The system supports incremental rebuilding, so it should be very fast to
29 change a single file and then test it out and have immediate feedback.
30
31 This requires for the right toolchain and qemu to be installed prior.
32 I've had success with those from musl.cc.
33
34 This is tailored for WireGuard at the moment, though later projects
35 might generalize it for other network testing.
36
37 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
38 Signed-off-by: David S. Miller <davem@davemloft.net>
39 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
40 ---
41 .../selftests/wireguard/qemu/.gitignore | 2 +
42 .../testing/selftests/wireguard/qemu/Makefile | 385 ++++++++++++++++++
43 .../wireguard/qemu/arch/aarch64.config | 5 +
44 .../wireguard/qemu/arch/aarch64_be.config | 6 +
45 .../selftests/wireguard/qemu/arch/arm.config | 9 +
46 .../wireguard/qemu/arch/armeb.config | 10 +
47 .../selftests/wireguard/qemu/arch/i686.config | 5 +
48 .../selftests/wireguard/qemu/arch/m68k.config | 9 +
49 .../selftests/wireguard/qemu/arch/mips.config | 11 +
50 .../wireguard/qemu/arch/mips64.config | 14 +
51 .../wireguard/qemu/arch/mips64el.config | 15 +
52 .../wireguard/qemu/arch/mipsel.config | 12 +
53 .../wireguard/qemu/arch/powerpc.config | 10 +
54 .../wireguard/qemu/arch/powerpc64le.config | 12 +
55 .../wireguard/qemu/arch/x86_64.config | 5 +
56 .../selftests/wireguard/qemu/debug.config | 67 +++
57 tools/testing/selftests/wireguard/qemu/init.c | 284 +++++++++++++
58 .../selftests/wireguard/qemu/kernel.config | 86 ++++
59 18 files changed, 947 insertions(+)
60 create mode 100644 tools/testing/selftests/wireguard/qemu/.gitignore
61 create mode 100644 tools/testing/selftests/wireguard/qemu/Makefile
62 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/aarch64.config
63 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/aarch64_be.config
64 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/arm.config
65 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/armeb.config
66 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/i686.config
67 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/m68k.config
68 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/mips.config
69 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/mips64.config
70 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/mips64el.config
71 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/mipsel.config
72 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/powerpc.config
73 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config
74 create mode 100644 tools/testing/selftests/wireguard/qemu/arch/x86_64.config
75 create mode 100644 tools/testing/selftests/wireguard/qemu/debug.config
76 create mode 100644 tools/testing/selftests/wireguard/qemu/init.c
77 create mode 100644 tools/testing/selftests/wireguard/qemu/kernel.config
78
79 --- /dev/null
80 +++ b/tools/testing/selftests/wireguard/qemu/.gitignore
81 @@ -0,0 +1,2 @@
82 +build/
83 +distfiles/
84 --- /dev/null
85 +++ b/tools/testing/selftests/wireguard/qemu/Makefile
86 @@ -0,0 +1,385 @@
87 +# SPDX-License-Identifier: GPL-2.0
88 +#
89 +# Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
90 +
91 +PWD := $(shell pwd)
92 +
93 +CHOST := $(shell gcc -dumpmachine)
94 +ifneq (,$(ARCH))
95 +CBUILD := $(subst -gcc,,$(lastword $(subst /, ,$(firstword $(wildcard $(foreach bindir,$(subst :, ,$(PATH)),$(bindir)/$(ARCH)-*-gcc))))))
96 +ifeq (,$(CBUILD))
97 +$(error The toolchain for $(ARCH) is not installed)
98 +endif
99 +else
100 +CBUILD := $(CHOST)
101 +ARCH := $(firstword $(subst -, ,$(CBUILD)))
102 +endif
103 +
104 +# Set these from the environment to override
105 +KERNEL_PATH ?= $(PWD)/../../../../..
106 +BUILD_PATH ?= $(PWD)/build/$(ARCH)
107 +DISTFILES_PATH ?= $(PWD)/distfiles
108 +NR_CPUS ?= 4
109 +
110 +MIRROR := https://download.wireguard.com/qemu-test/distfiles/
111 +
112 +default: qemu
113 +
114 +# variable name, tarball project name, version, tarball extension, default URI base
115 +define tar_download =
116 +$(1)_VERSION := $(3)
117 +$(1)_NAME := $(2)-$$($(1)_VERSION)
118 +$(1)_TAR := $(DISTFILES_PATH)/$$($(1)_NAME)$(4)
119 +$(1)_PATH := $(BUILD_PATH)/$$($(1)_NAME)
120 +$(call file_download,$$($(1)_NAME)$(4),$(5),$(6))
121 +endef
122 +
123 +define file_download =
124 +$(DISTFILES_PATH)/$(1):
125 + mkdir -p $(DISTFILES_PATH)
126 + flock -x $$@.lock -c '[ -f $$@ ] && exit 0; wget -O $$@.tmp $(MIRROR)$(1) || wget -t inf --retry-on-http-error=404 -O $$@.tmp $(2)$(1) || rm -f $$@.tmp'
127 + if echo "$(3) $$@.tmp" | sha256sum -c -; then mv $$@.tmp $$@; else rm -f $$@.tmp; exit 71; fi
128 +endef
129 +
130 +$(eval $(call tar_download,MUSL,musl,1.1.20,.tar.gz,https://www.musl-libc.org/releases/,44be8771d0e6c6b5f82dd15662eb2957c9a3173a19a8b49966ac0542bbd40d61))
131 +$(eval $(call tar_download,LIBMNL,libmnl,1.0.4,.tar.bz2,https://www.netfilter.org/projects/libmnl/files/,171f89699f286a5854b72b91d06e8f8e3683064c5901fb09d954a9ab6f551f81))
132 +$(eval $(call tar_download,IPERF,iperf,3.1.7,.tar.gz,http://downloads.es.net/pub/iperf/,a4ef73406fe92250602b8da2ae89ec53211f805df97a1d1d629db5a14043734f))
133 +$(eval $(call tar_download,BASH,bash,5.0,.tar.gz,https://ftp.gnu.org/gnu/bash/,b4a80f2ac66170b2913efbfb9f2594f1f76c7b1afd11f799e22035d63077fb4d))
134 +$(eval $(call tar_download,IPROUTE2,iproute2,5.1.0,.tar.gz,https://www.kernel.org/pub/linux/utils/net/iproute2/,9b43707d6075ecdca14803ca8ce0c8553848c49fa1586d12fd508d66577243f2))
135 +$(eval $(call tar_download,IPTABLES,iptables,1.6.1,.tar.bz2,https://www.netfilter.org/projects/iptables/files/,0fc2d7bd5d7be11311726466789d4c65fb4c8e096c9182b56ce97440864f0cf5))
136 +$(eval $(call tar_download,NMAP,nmap,7.60,.tar.bz2,https://nmap.org/dist/,a8796ecc4fa6c38aad6139d9515dc8113023a82e9d787e5a5fb5fa1b05516f21))
137 +$(eval $(call tar_download,IPUTILS,iputils,s20161105,.tar.gz,https://github.com/iputils/iputils/archive/s20161105.tar.gz/#,f813092f03d17294fd23544b129b95cdb87fe19f7970a51908a6b88509acad8a))
138 +$(eval $(call tar_download,WIREGUARD_TOOLS,WireGuard,0.0.20191212,.tar.xz,https://git.zx2c4.com/WireGuard/snapshot/,b0d718380f7a8822b2f12d75e462fa4eafa3a77871002981f367cd4fe2a1b071))
139 +
140 +KERNEL_BUILD_PATH := $(BUILD_PATH)/kernel$(if $(findstring yes,$(DEBUG_KERNEL)),-debug)
141 +rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
142 +WIREGUARD_SOURCES := $(call rwildcard,$(KERNEL_PATH)/drivers/net/wireguard/,*)
143 +
144 +export CFLAGS ?= -O3 -pipe
145 +export LDFLAGS ?=
146 +export CPPFLAGS := -I$(BUILD_PATH)/include
147 +
148 +ifeq ($(CHOST),$(CBUILD))
149 +CROSS_COMPILE_FLAG := --host=$(CHOST)
150 +NOPIE_GCC := gcc -fno-PIE
151 +CFLAGS += -march=native
152 +STRIP := strip
153 +else
154 +$(info Cross compilation: building for $(CBUILD) using $(CHOST))
155 +CROSS_COMPILE_FLAG := --build=$(CBUILD) --host=$(CHOST)
156 +export CROSS_COMPILE=$(CBUILD)-
157 +NOPIE_GCC := $(CBUILD)-gcc -fno-PIE
158 +STRIP := $(CBUILD)-strip
159 +endif
160 +ifeq ($(ARCH),aarch64)
161 +QEMU_ARCH := aarch64
162 +KERNEL_ARCH := arm64
163 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm64/boot/Image
164 +ifeq ($(CHOST),$(CBUILD))
165 +QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
166 +else
167 +QEMU_MACHINE := -cpu cortex-a53 -machine virt
168 +CFLAGS += -march=armv8-a -mtune=cortex-a53
169 +endif
170 +else ifeq ($(ARCH),aarch64_be)
171 +QEMU_ARCH := aarch64
172 +KERNEL_ARCH := arm64
173 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm64/boot/Image
174 +ifeq ($(CHOST),$(CBUILD))
175 +QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
176 +else
177 +QEMU_MACHINE := -cpu cortex-a53 -machine virt
178 +CFLAGS += -march=armv8-a -mtune=cortex-a53
179 +endif
180 +else ifeq ($(ARCH),arm)
181 +QEMU_ARCH := arm
182 +KERNEL_ARCH := arm
183 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm/boot/zImage
184 +ifeq ($(CHOST),$(CBUILD))
185 +QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
186 +else
187 +QEMU_MACHINE := -cpu cortex-a15 -machine virt
188 +CFLAGS += -march=armv7-a -mtune=cortex-a15 -mabi=aapcs-linux
189 +endif
190 +else ifeq ($(ARCH),armeb)
191 +QEMU_ARCH := arm
192 +KERNEL_ARCH := arm
193 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm/boot/zImage
194 +ifeq ($(CHOST),$(CBUILD))
195 +QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
196 +else
197 +QEMU_MACHINE := -cpu cortex-a15 -machine virt
198 +CFLAGS += -march=armv7-a -mabi=aapcs-linux # We don't pass -mtune=cortex-a15 due to a compiler bug on big endian.
199 +LDFLAGS += -Wl,--be8
200 +endif
201 +else ifeq ($(ARCH),x86_64)
202 +QEMU_ARCH := x86_64
203 +KERNEL_ARCH := x86_64
204 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x86/boot/bzImage
205 +ifeq ($(CHOST),$(CBUILD))
206 +QEMU_MACHINE := -cpu host -machine q35,accel=kvm
207 +else
208 +QEMU_MACHINE := -cpu Skylake-Server -machine q35
209 +CFLAGS += -march=skylake-avx512
210 +endif
211 +else ifeq ($(ARCH),i686)
212 +QEMU_ARCH := i386
213 +KERNEL_ARCH := x86
214 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x86/boot/bzImage
215 +ifeq ($(subst i686,x86_64,$(CBUILD)),$(CHOST))
216 +QEMU_MACHINE := -cpu host -machine q35,accel=kvm
217 +else
218 +QEMU_MACHINE := -cpu coreduo -machine q35
219 +CFLAGS += -march=prescott
220 +endif
221 +else ifeq ($(ARCH),mips64)
222 +QEMU_ARCH := mips64
223 +KERNEL_ARCH := mips
224 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
225 +ifeq ($(CHOST),$(CBUILD))
226 +QEMU_MACHINE := -cpu host -machine malta,accel=kvm
227 +CFLAGS += -EB
228 +else
229 +QEMU_MACHINE := -cpu MIPS64R2-generic -machine malta -smp 1
230 +CFLAGS += -march=mips64r2 -EB
231 +endif
232 +else ifeq ($(ARCH),mips64el)
233 +QEMU_ARCH := mips64el
234 +KERNEL_ARCH := mips
235 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
236 +ifeq ($(CHOST),$(CBUILD))
237 +QEMU_MACHINE := -cpu host -machine malta,accel=kvm
238 +CFLAGS += -EL
239 +else
240 +QEMU_MACHINE := -cpu MIPS64R2-generic -machine malta -smp 1
241 +CFLAGS += -march=mips64r2 -EL
242 +endif
243 +else ifeq ($(ARCH),mips)
244 +QEMU_ARCH := mips
245 +KERNEL_ARCH := mips
246 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
247 +ifeq ($(CHOST),$(CBUILD))
248 +QEMU_MACHINE := -cpu host -machine malta,accel=kvm
249 +CFLAGS += -EB
250 +else
251 +QEMU_MACHINE := -cpu 24Kf -machine malta -smp 1
252 +CFLAGS += -march=mips32r2 -EB
253 +endif
254 +else ifeq ($(ARCH),mipsel)
255 +QEMU_ARCH := mipsel
256 +KERNEL_ARCH := mips
257 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
258 +ifeq ($(CHOST),$(CBUILD))
259 +QEMU_MACHINE := -cpu host -machine malta,accel=kvm
260 +CFLAGS += -EL
261 +else
262 +QEMU_MACHINE := -cpu 24Kf -machine malta -smp 1
263 +CFLAGS += -march=mips32r2 -EL
264 +endif
265 +else ifeq ($(ARCH),powerpc64le)
266 +QEMU_ARCH := ppc64
267 +KERNEL_ARCH := powerpc
268 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
269 +ifeq ($(CHOST),$(CBUILD))
270 +QEMU_MACHINE := -cpu host,accel=kvm -machine pseries
271 +else
272 +QEMU_MACHINE := -machine pseries
273 +endif
274 +CFLAGS += -mcpu=powerpc64le -mlong-double-64
275 +else ifeq ($(ARCH),powerpc)
276 +QEMU_ARCH := ppc
277 +KERNEL_ARCH := powerpc
278 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/powerpc/boot/uImage
279 +ifeq ($(CHOST),$(CBUILD))
280 +QEMU_MACHINE := -cpu host,accel=kvm -machine ppce500
281 +else
282 +QEMU_MACHINE := -machine ppce500
283 +endif
284 +CFLAGS += -mcpu=powerpc -mlong-double-64 -msecure-plt
285 +else ifeq ($(ARCH),m68k)
286 +QEMU_ARCH := m68k
287 +KERNEL_ARCH := m68k
288 +KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
289 +ifeq ($(CHOST),$(CBUILD))
290 +QEMU_MACHINE := -cpu host,accel=kvm -machine q800
291 +else
292 +QEMU_MACHINE := -machine q800
293 +endif
294 +else
295 +$(error I only build: x86_64, i686, arm, armeb, aarch64, aarch64_be, mips, mipsel, mips64, mips64el, powerpc64le, powerpc, m68k)
296 +endif
297 +
298 +REAL_CC := $(CBUILD)-gcc
299 +MUSL_CC := $(BUILD_PATH)/musl-gcc
300 +export CC := $(MUSL_CC)
301 +USERSPACE_DEPS := $(MUSL_CC) $(BUILD_PATH)/include/.installed $(BUILD_PATH)/include/linux/.installed
302 +
303 +build: $(KERNEL_BZIMAGE)
304 +qemu: $(KERNEL_BZIMAGE)
305 + rm -f $(BUILD_PATH)/result
306 + timeout --foreground 20m qemu-system-$(QEMU_ARCH) \
307 + -nodefaults \
308 + -nographic \
309 + -smp $(NR_CPUS) \
310 + $(QEMU_MACHINE) \
311 + -m $$(grep -q CONFIG_DEBUG_KMEMLEAK=y $(KERNEL_BUILD_PATH)/.config && echo 1G || echo 256M) \
312 + -serial stdio \
313 + -serial file:$(BUILD_PATH)/result \
314 + -no-reboot \
315 + -monitor none \
316 + -kernel $<
317 + grep -Fq success $(BUILD_PATH)/result
318 +
319 +$(BUILD_PATH)/init-cpio-spec.txt:
320 + mkdir -p $(BUILD_PATH)
321 + echo "file /init $(BUILD_PATH)/init 755 0 0" > $@
322 + echo "file /init.sh $(PWD)/../netns.sh 755 0 0" >> $@
323 + echo "dir /dev 755 0 0" >> $@
324 + echo "nod /dev/console 644 0 0 c 5 1" >> $@
325 + echo "dir /bin 755 0 0" >> $@
326 + echo "file /bin/iperf3 $(IPERF_PATH)/src/iperf3 755 0 0" >> $@
327 + echo "file /bin/wg $(WIREGUARD_TOOLS_PATH)/src/tools/wg 755 0 0" >> $@
328 + echo "file /bin/bash $(BASH_PATH)/bash 755 0 0" >> $@
329 + echo "file /bin/ip $(IPROUTE2_PATH)/ip/ip 755 0 0" >> $@
330 + echo "file /bin/ss $(IPROUTE2_PATH)/misc/ss 755 0 0" >> $@
331 + echo "file /bin/ping $(IPUTILS_PATH)/ping 755 0 0" >> $@
332 + echo "file /bin/ncat $(NMAP_PATH)/ncat/ncat 755 0 0" >> $@
333 + echo "file /bin/xtables-multi $(IPTABLES_PATH)/iptables/xtables-multi 755 0 0" >> $@
334 + echo "slink /bin/iptables xtables-multi 777 0 0" >> $@
335 + echo "slink /bin/ping6 ping 777 0 0" >> $@
336 + echo "dir /lib 755 0 0" >> $@
337 + echo "file /lib/libc.so $(MUSL_PATH)/lib/libc.so 755 0 0" >> $@
338 + echo "slink /lib/ld-linux.so.1 libc.so 777 0 0" >> $@
339 +
340 +$(KERNEL_BUILD_PATH)/.config: kernel.config arch/$(ARCH).config
341 + mkdir -p $(KERNEL_BUILD_PATH)
342 + cp kernel.config $(KERNEL_BUILD_PATH)/minimal.config
343 + printf 'CONFIG_NR_CPUS=$(NR_CPUS)\nCONFIG_INITRAMFS_SOURCE="$(BUILD_PATH)/init-cpio-spec.txt"\n' >> $(KERNEL_BUILD_PATH)/minimal.config
344 + cat arch/$(ARCH).config >> $(KERNEL_BUILD_PATH)/minimal.config
345 + $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) allnoconfig
346 + cd $(KERNEL_BUILD_PATH) && ARCH=$(KERNEL_ARCH) $(KERNEL_PATH)/scripts/kconfig/merge_config.sh -n $(KERNEL_BUILD_PATH)/.config $(KERNEL_BUILD_PATH)/minimal.config
347 + $(if $(findstring yes,$(DEBUG_KERNEL)),cp debug.config $(KERNEL_BUILD_PATH) && cd $(KERNEL_BUILD_PATH) && ARCH=$(KERNEL_ARCH) $(KERNEL_PATH)/scripts/kconfig/merge_config.sh -n $(KERNEL_BUILD_PATH)/.config debug.config,)
348 +
349 +$(KERNEL_BZIMAGE): $(KERNEL_BUILD_PATH)/.config $(BUILD_PATH)/init-cpio-spec.txt $(MUSL_PATH)/lib/libc.so $(IPERF_PATH)/src/iperf3 $(IPUTILS_PATH)/ping $(BASH_PATH)/bash $(IPROUTE2_PATH)/misc/ss $(IPROUTE2_PATH)/ip/ip $(IPTABLES_PATH)/iptables/xtables-multi $(NMAP_PATH)/ncat/ncat $(WIREGUARD_TOOLS_PATH)/src/tools/wg $(BUILD_PATH)/init ../netns.sh $(WIREGUARD_SOURCES)
350 + $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) CC="$(NOPIE_GCC)"
351 +
352 +$(BUILD_PATH)/include/linux/.installed: | $(KERNEL_BUILD_PATH)/.config
353 + $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) INSTALL_HDR_PATH=$(BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) headers_install
354 + touch $@
355 +
356 +$(MUSL_PATH)/lib/libc.so: $(MUSL_TAR)
357 + mkdir -p $(BUILD_PATH)
358 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
359 + cd $(MUSL_PATH) && CC=$(REAL_CC) ./configure --prefix=/ --disable-static --build=$(CBUILD)
360 + $(MAKE) -C $(MUSL_PATH)
361 + $(STRIP) -s $@
362 +
363 +$(BUILD_PATH)/include/.installed: $(MUSL_PATH)/lib/libc.so
364 + $(MAKE) -C $(MUSL_PATH) DESTDIR=$(BUILD_PATH) install-headers
365 + touch $@
366 +
367 +$(MUSL_CC): $(MUSL_PATH)/lib/libc.so
368 + sh $(MUSL_PATH)/tools/musl-gcc.specs.sh $(BUILD_PATH)/include $(MUSL_PATH)/lib /lib/ld-linux.so.1 > $(BUILD_PATH)/musl-gcc.specs
369 + printf '#!/bin/sh\nexec "$(REAL_CC)" --specs="$(BUILD_PATH)/musl-gcc.specs" -fno-stack-protector -no-pie "$$@"\n' > $(BUILD_PATH)/musl-gcc
370 + chmod +x $(BUILD_PATH)/musl-gcc
371 +
372 +$(IPERF_PATH)/.installed: $(IPERF_TAR)
373 + mkdir -p $(BUILD_PATH)
374 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
375 + sed -i '1s/^/#include <stdint.h>/' $(IPERF_PATH)/src/cjson.h $(IPERF_PATH)/src/timer.h
376 + sed -i -r 's/-p?g//g' $(IPERF_PATH)/src/Makefile*
377 + touch $@
378 +
379 +$(IPERF_PATH)/src/iperf3: | $(IPERF_PATH)/.installed $(USERSPACE_DEPS)
380 + cd $(IPERF_PATH) && CFLAGS="$(CFLAGS) -D_GNU_SOURCE" ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared
381 + $(MAKE) -C $(IPERF_PATH)
382 + $(STRIP) -s $@
383 +
384 +$(LIBMNL_PATH)/.installed: $(LIBMNL_TAR)
385 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
386 + touch $@
387 +
388 +$(LIBMNL_PATH)/src/.libs/libmnl.a: | $(LIBMNL_PATH)/.installed $(USERSPACE_DEPS)
389 + cd $(LIBMNL_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared
390 + $(MAKE) -C $(LIBMNL_PATH)
391 + sed -i 's:prefix=.*:prefix=$(LIBMNL_PATH):' $(LIBMNL_PATH)/libmnl.pc
392 +
393 +$(WIREGUARD_TOOLS_PATH)/.installed: $(WIREGUARD_TOOLS_TAR)
394 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
395 + touch $@
396 +
397 +$(WIREGUARD_TOOLS_PATH)/src/tools/wg: | $(WIREGUARD_TOOLS_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
398 + LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" $(MAKE) -C $(WIREGUARD_TOOLS_PATH)/src/tools LIBMNL_CFLAGS="-I$(LIBMNL_PATH)/include" LIBMNL_LDLIBS="-lmnl" wg
399 + $(STRIP) -s $@
400 +
401 +$(BUILD_PATH)/init: init.c | $(USERSPACE_DEPS)
402 + mkdir -p $(BUILD_PATH)
403 + $(MUSL_CC) -o $@ $(CFLAGS) $(LDFLAGS) -std=gnu11 $<
404 + $(STRIP) -s $@
405 +
406 +$(IPUTILS_PATH)/.installed: $(IPUTILS_TAR)
407 + mkdir -p $(BUILD_PATH)
408 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
409 + touch $@
410 +
411 +$(IPUTILS_PATH)/ping: | $(IPUTILS_PATH)/.installed $(USERSPACE_DEPS)
412 + $(MAKE) -C $(IPUTILS_PATH) USE_CAP=no USE_IDN=no USE_NETTLE=no USE_CRYPTO=no ping
413 + $(STRIP) -s $@
414 +
415 +$(BASH_PATH)/.installed: $(BASH_TAR)
416 + mkdir -p $(BUILD_PATH)
417 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
418 + touch $@
419 +
420 +$(BASH_PATH)/bash: | $(BASH_PATH)/.installed $(USERSPACE_DEPS)
421 + cd $(BASH_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --without-bash-malloc --disable-debugger --disable-help-builtin --disable-history --disable-multibyte --disable-progcomp --disable-readline --disable-mem-scramble
422 + $(MAKE) -C $(BASH_PATH)
423 + $(STRIP) -s $@
424 +
425 +$(IPROUTE2_PATH)/.installed: $(IPROUTE2_TAR)
426 + mkdir -p $(BUILD_PATH)
427 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
428 + printf 'CC:=$(CC)\nPKG_CONFIG:=pkg-config\nTC_CONFIG_XT:=n\nTC_CONFIG_ATM:=n\nTC_CONFIG_IPSET:=n\nIP_CONFIG_SETNS:=y\nHAVE_ELF:=n\nHAVE_MNL:=y\nHAVE_BERKELEY_DB:=n\nHAVE_LATEX:=n\nHAVE_PDFLATEX:=n\nCFLAGS+=-DHAVE_SETNS -DHAVE_LIBMNL -I$(LIBMNL_PATH)/include\nLDLIBS+=-lmnl' > $(IPROUTE2_PATH)/config.mk
429 + printf 'lib: snapshot\n\t$$(MAKE) -C lib\nip/ip: lib\n\t$$(MAKE) -C ip ip\nmisc/ss: lib\n\t$$(MAKE) -C misc ss\n' >> $(IPROUTE2_PATH)/Makefile
430 + touch $@
431 +
432 +$(IPROUTE2_PATH)/ip/ip: | $(IPROUTE2_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
433 + LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" $(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ ip/ip
434 + $(STRIP) -s $(IPROUTE2_PATH)/ip/ip
435 +
436 +$(IPROUTE2_PATH)/misc/ss: | $(IPROUTE2_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
437 + LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" $(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ misc/ss
438 + $(STRIP) -s $(IPROUTE2_PATH)/misc/ss
439 +
440 +$(IPTABLES_PATH)/.installed: $(IPTABLES_TAR)
441 + mkdir -p $(BUILD_PATH)
442 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
443 + sed -i -e "/nfnetlink=[01]/s:=[01]:=0:" -e "/nfconntrack=[01]/s:=[01]:=0:" $(IPTABLES_PATH)/configure
444 + touch $@
445 +
446 +$(IPTABLES_PATH)/iptables/xtables-multi: | $(IPTABLES_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
447 + cd $(IPTABLES_PATH) && PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --disable-nftables --disable-bpf-compiler --disable-nfsynproxy --disable-libipq --with-kernel=$(BUILD_PATH)/include
448 + $(MAKE) -C $(IPTABLES_PATH)
449 + $(STRIP) -s $@
450 +
451 +$(NMAP_PATH)/.installed: $(NMAP_TAR)
452 + mkdir -p $(BUILD_PATH)
453 + flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
454 + touch $@
455 +
456 +$(NMAP_PATH)/ncat/ncat: | $(NMAP_PATH)/.installed $(USERSPACE_DEPS)
457 + cd $(NMAP_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --without-ndiff --without-zenmap --without-nping --with-libpcap=included --with-libpcre=included --with-libdnet=included --without-liblua --with-liblinear=included --without-nmap-update --without-openssl --with-pcap=linux
458 + $(MAKE) -C $(NMAP_PATH) build-ncat
459 + $(STRIP) -s $@
460 +
461 +clean:
462 + rm -rf $(BUILD_PATH)
463 +
464 +distclean: clean
465 + rm -rf $(DISTFILES_PATH)
466 +
467 +menuconfig: $(KERNEL_BUILD_PATH)/.config
468 + $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) CC="$(NOPIE_GCC)" menuconfig
469 +
470 +.PHONY: qemu build clean distclean menuconfig
471 +.DELETE_ON_ERROR:
472 --- /dev/null
473 +++ b/tools/testing/selftests/wireguard/qemu/arch/aarch64.config
474 @@ -0,0 +1,5 @@
475 +CONFIG_SERIAL_AMBA_PL011=y
476 +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
477 +CONFIG_CMDLINE_BOOL=y
478 +CONFIG_CMDLINE="console=ttyAMA0 wg.success=ttyAMA1"
479 +CONFIG_FRAME_WARN=1280
480 --- /dev/null
481 +++ b/tools/testing/selftests/wireguard/qemu/arch/aarch64_be.config
482 @@ -0,0 +1,6 @@
483 +CONFIG_CPU_BIG_ENDIAN=y
484 +CONFIG_SERIAL_AMBA_PL011=y
485 +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
486 +CONFIG_CMDLINE_BOOL=y
487 +CONFIG_CMDLINE="console=ttyAMA0 wg.success=ttyAMA1"
488 +CONFIG_FRAME_WARN=1280
489 --- /dev/null
490 +++ b/tools/testing/selftests/wireguard/qemu/arch/arm.config
491 @@ -0,0 +1,9 @@
492 +CONFIG_MMU=y
493 +CONFIG_ARCH_MULTI_V7=y
494 +CONFIG_ARCH_VIRT=y
495 +CONFIG_THUMB2_KERNEL=n
496 +CONFIG_SERIAL_AMBA_PL011=y
497 +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
498 +CONFIG_CMDLINE_BOOL=y
499 +CONFIG_CMDLINE="console=ttyAMA0 wg.success=ttyAMA1"
500 +CONFIG_FRAME_WARN=1024
501 --- /dev/null
502 +++ b/tools/testing/selftests/wireguard/qemu/arch/armeb.config
503 @@ -0,0 +1,10 @@
504 +CONFIG_MMU=y
505 +CONFIG_ARCH_MULTI_V7=y
506 +CONFIG_ARCH_VIRT=y
507 +CONFIG_THUMB2_KERNEL=n
508 +CONFIG_SERIAL_AMBA_PL011=y
509 +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
510 +CONFIG_CMDLINE_BOOL=y
511 +CONFIG_CMDLINE="console=ttyAMA0 wg.success=ttyAMA1"
512 +CONFIG_CPU_BIG_ENDIAN=y
513 +CONFIG_FRAME_WARN=1024
514 --- /dev/null
515 +++ b/tools/testing/selftests/wireguard/qemu/arch/i686.config
516 @@ -0,0 +1,5 @@
517 +CONFIG_SERIAL_8250=y
518 +CONFIG_SERIAL_8250_CONSOLE=y
519 +CONFIG_CMDLINE_BOOL=y
520 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
521 +CONFIG_FRAME_WARN=1024
522 --- /dev/null
523 +++ b/tools/testing/selftests/wireguard/qemu/arch/m68k.config
524 @@ -0,0 +1,9 @@
525 +CONFIG_MMU=y
526 +CONFIG_M68040=y
527 +CONFIG_MAC=y
528 +CONFIG_SERIAL_PMACZILOG=y
529 +CONFIG_SERIAL_PMACZILOG_TTYS=y
530 +CONFIG_SERIAL_PMACZILOG_CONSOLE=y
531 +CONFIG_CMDLINE_BOOL=y
532 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
533 +CONFIG_FRAME_WARN=1024
534 --- /dev/null
535 +++ b/tools/testing/selftests/wireguard/qemu/arch/mips.config
536 @@ -0,0 +1,11 @@
537 +CONFIG_CPU_MIPS32_R2=y
538 +CONFIG_MIPS_MALTA=y
539 +CONFIG_MIPS_CPS=y
540 +CONFIG_MIPS_FP_SUPPORT=y
541 +CONFIG_POWER_RESET=y
542 +CONFIG_POWER_RESET_SYSCON=y
543 +CONFIG_SERIAL_8250=y
544 +CONFIG_SERIAL_8250_CONSOLE=y
545 +CONFIG_CMDLINE_BOOL=y
546 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
547 +CONFIG_FRAME_WARN=1024
548 --- /dev/null
549 +++ b/tools/testing/selftests/wireguard/qemu/arch/mips64.config
550 @@ -0,0 +1,14 @@
551 +CONFIG_64BIT=y
552 +CONFIG_CPU_MIPS64_R2=y
553 +CONFIG_MIPS32_N32=y
554 +CONFIG_CPU_HAS_MSA=y
555 +CONFIG_MIPS_MALTA=y
556 +CONFIG_MIPS_CPS=y
557 +CONFIG_MIPS_FP_SUPPORT=y
558 +CONFIG_POWER_RESET=y
559 +CONFIG_POWER_RESET_SYSCON=y
560 +CONFIG_SERIAL_8250=y
561 +CONFIG_SERIAL_8250_CONSOLE=y
562 +CONFIG_CMDLINE_BOOL=y
563 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
564 +CONFIG_FRAME_WARN=1280
565 --- /dev/null
566 +++ b/tools/testing/selftests/wireguard/qemu/arch/mips64el.config
567 @@ -0,0 +1,15 @@
568 +CONFIG_64BIT=y
569 +CONFIG_CPU_MIPS64_R2=y
570 +CONFIG_MIPS32_N32=y
571 +CONFIG_CPU_HAS_MSA=y
572 +CONFIG_MIPS_MALTA=y
573 +CONFIG_CPU_LITTLE_ENDIAN=y
574 +CONFIG_MIPS_CPS=y
575 +CONFIG_MIPS_FP_SUPPORT=y
576 +CONFIG_POWER_RESET=y
577 +CONFIG_POWER_RESET_SYSCON=y
578 +CONFIG_SERIAL_8250=y
579 +CONFIG_SERIAL_8250_CONSOLE=y
580 +CONFIG_CMDLINE_BOOL=y
581 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
582 +CONFIG_FRAME_WARN=1280
583 --- /dev/null
584 +++ b/tools/testing/selftests/wireguard/qemu/arch/mipsel.config
585 @@ -0,0 +1,12 @@
586 +CONFIG_CPU_MIPS32_R2=y
587 +CONFIG_MIPS_MALTA=y
588 +CONFIG_CPU_LITTLE_ENDIAN=y
589 +CONFIG_MIPS_CPS=y
590 +CONFIG_MIPS_FP_SUPPORT=y
591 +CONFIG_POWER_RESET=y
592 +CONFIG_POWER_RESET_SYSCON=y
593 +CONFIG_SERIAL_8250=y
594 +CONFIG_SERIAL_8250_CONSOLE=y
595 +CONFIG_CMDLINE_BOOL=y
596 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
597 +CONFIG_FRAME_WARN=1024
598 --- /dev/null
599 +++ b/tools/testing/selftests/wireguard/qemu/arch/powerpc.config
600 @@ -0,0 +1,10 @@
601 +CONFIG_PPC_QEMU_E500=y
602 +CONFIG_FSL_SOC_BOOKE=y
603 +CONFIG_PPC_85xx=y
604 +CONFIG_PHYS_64BIT=y
605 +CONFIG_SERIAL_8250=y
606 +CONFIG_SERIAL_8250_CONSOLE=y
607 +CONFIG_MATH_EMULATION=y
608 +CONFIG_CMDLINE_BOOL=y
609 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
610 +CONFIG_FRAME_WARN=1024
611 --- /dev/null
612 +++ b/tools/testing/selftests/wireguard/qemu/arch/powerpc64le.config
613 @@ -0,0 +1,12 @@
614 +CONFIG_PPC64=y
615 +CONFIG_PPC_PSERIES=y
616 +CONFIG_ALTIVEC=y
617 +CONFIG_VSX=y
618 +CONFIG_PPC_OF_BOOT_TRAMPOLINE=y
619 +CONFIG_PPC_RADIX_MMU=y
620 +CONFIG_HVC_CONSOLE=y
621 +CONFIG_CPU_LITTLE_ENDIAN=y
622 +CONFIG_CMDLINE_BOOL=y
623 +CONFIG_CMDLINE="console=hvc0 wg.success=hvc1"
624 +CONFIG_SECTION_MISMATCH_WARN_ONLY=y
625 +CONFIG_FRAME_WARN=1280
626 --- /dev/null
627 +++ b/tools/testing/selftests/wireguard/qemu/arch/x86_64.config
628 @@ -0,0 +1,5 @@
629 +CONFIG_SERIAL_8250=y
630 +CONFIG_SERIAL_8250_CONSOLE=y
631 +CONFIG_CMDLINE_BOOL=y
632 +CONFIG_CMDLINE="console=ttyS0 wg.success=ttyS1"
633 +CONFIG_FRAME_WARN=1280
634 --- /dev/null
635 +++ b/tools/testing/selftests/wireguard/qemu/debug.config
636 @@ -0,0 +1,67 @@
637 +CONFIG_LOCALVERSION="-debug"
638 +CONFIG_ENABLE_WARN_DEPRECATED=y
639 +CONFIG_ENABLE_MUST_CHECK=y
640 +CONFIG_FRAME_POINTER=y
641 +CONFIG_STACK_VALIDATION=y
642 +CONFIG_DEBUG_KERNEL=y
643 +CONFIG_DEBUG_INFO=y
644 +CONFIG_DEBUG_INFO_DWARF4=y
645 +CONFIG_PAGE_EXTENSION=y
646 +CONFIG_PAGE_POISONING=y
647 +CONFIG_DEBUG_OBJECTS=y
648 +CONFIG_DEBUG_OBJECTS_FREE=y
649 +CONFIG_DEBUG_OBJECTS_TIMERS=y
650 +CONFIG_DEBUG_OBJECTS_WORK=y
651 +CONFIG_DEBUG_OBJECTS_RCU_HEAD=y
652 +CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER=y
653 +CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT=1
654 +CONFIG_SLUB_DEBUG_ON=y
655 +CONFIG_DEBUG_VM=y
656 +CONFIG_DEBUG_MEMORY_INIT=y
657 +CONFIG_HAVE_DEBUG_STACKOVERFLOW=y
658 +CONFIG_DEBUG_STACKOVERFLOW=y
659 +CONFIG_HAVE_ARCH_KMEMCHECK=y
660 +CONFIG_HAVE_ARCH_KASAN=y
661 +CONFIG_KASAN=y
662 +CONFIG_KASAN_INLINE=y
663 +CONFIG_UBSAN=y
664 +CONFIG_UBSAN_SANITIZE_ALL=y
665 +CONFIG_UBSAN_NO_ALIGNMENT=y
666 +CONFIG_UBSAN_NULL=y
667 +CONFIG_DEBUG_KMEMLEAK=y
668 +CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=8192
669 +CONFIG_DEBUG_STACK_USAGE=y
670 +CONFIG_DEBUG_SHIRQ=y
671 +CONFIG_WQ_WATCHDOG=y
672 +CONFIG_SCHED_DEBUG=y
673 +CONFIG_SCHED_INFO=y
674 +CONFIG_SCHEDSTATS=y
675 +CONFIG_SCHED_STACK_END_CHECK=y
676 +CONFIG_DEBUG_TIMEKEEPING=y
677 +CONFIG_TIMER_STATS=y
678 +CONFIG_DEBUG_PREEMPT=y
679 +CONFIG_DEBUG_RT_MUTEXES=y
680 +CONFIG_DEBUG_SPINLOCK=y
681 +CONFIG_DEBUG_MUTEXES=y
682 +CONFIG_DEBUG_LOCK_ALLOC=y
683 +CONFIG_PROVE_LOCKING=y
684 +CONFIG_LOCKDEP=y
685 +CONFIG_DEBUG_ATOMIC_SLEEP=y
686 +CONFIG_TRACE_IRQFLAGS=y
687 +CONFIG_DEBUG_BUGVERBOSE=y
688 +CONFIG_DEBUG_LIST=y
689 +CONFIG_DEBUG_PI_LIST=y
690 +CONFIG_PROVE_RCU=y
691 +CONFIG_SPARSE_RCU_POINTER=y
692 +CONFIG_RCU_CPU_STALL_TIMEOUT=21
693 +CONFIG_RCU_TRACE=y
694 +CONFIG_RCU_EQS_DEBUG=y
695 +CONFIG_USER_STACKTRACE_SUPPORT=y
696 +CONFIG_DEBUG_SG=y
697 +CONFIG_DEBUG_NOTIFIERS=y
698 +CONFIG_DOUBLEFAULT=y
699 +CONFIG_X86_DEBUG_FPU=y
700 +CONFIG_DEBUG_SECTION_MISMATCH=y
701 +CONFIG_DEBUG_PAGEALLOC=y
702 +CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=y
703 +CONFIG_DEBUG_WW_MUTEX_SLOWPATH=y
704 --- /dev/null
705 +++ b/tools/testing/selftests/wireguard/qemu/init.c
706 @@ -0,0 +1,284 @@
707 +// SPDX-License-Identifier: GPL-2.0
708 +/*
709 + * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
710 + */
711 +
712 +#define _GNU_SOURCE
713 +#include <unistd.h>
714 +#include <errno.h>
715 +#include <string.h>
716 +#include <stdio.h>
717 +#include <stdlib.h>
718 +#include <stdbool.h>
719 +#include <fcntl.h>
720 +#include <sys/wait.h>
721 +#include <sys/mount.h>
722 +#include <sys/types.h>
723 +#include <sys/stat.h>
724 +#include <sys/types.h>
725 +#include <sys/io.h>
726 +#include <sys/ioctl.h>
727 +#include <sys/reboot.h>
728 +#include <sys/utsname.h>
729 +#include <sys/sendfile.h>
730 +#include <linux/random.h>
731 +#include <linux/version.h>
732 +
733 +__attribute__((noreturn)) static void poweroff(void)
734 +{
735 + fflush(stdout);
736 + fflush(stderr);
737 + reboot(RB_AUTOBOOT);
738 + sleep(30);
739 + fprintf(stderr, "\x1b[37m\x1b[41m\x1b[1mFailed to power off!!!\x1b[0m\n");
740 + exit(1);
741 +}
742 +
743 +static void panic(const char *what)
744 +{
745 + fprintf(stderr, "\n\n\x1b[37m\x1b[41m\x1b[1mSOMETHING WENT HORRIBLY WRONG\x1b[0m\n\n \x1b[31m\x1b[1m%s: %s\x1b[0m\n\n\x1b[37m\x1b[44m\x1b[1mPower off...\x1b[0m\n\n", what, strerror(errno));
746 + poweroff();
747 +}
748 +
749 +#define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m")
750 +
751 +static void print_banner(void)
752 +{
753 + struct utsname utsname;
754 + int len;
755 +
756 + if (uname(&utsname) < 0)
757 + panic("uname");
758 +
759 + len = strlen(" WireGuard Test Suite on ") + strlen(utsname.sysname) + strlen(utsname.release) + strlen(utsname.machine);
760 + printf("\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\x1b[45m\x1b[33m\x1b[1m WireGuard Test Suite on %s %s %s \x1b[0m\n\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\n", len, "", utsname.sysname, utsname.release, utsname.machine, len, "");
761 +}
762 +
763 +static void seed_rng(void)
764 +{
765 + int fd;
766 + struct {
767 + int entropy_count;
768 + int buffer_size;
769 + unsigned char buffer[256];
770 + } entropy = {
771 + .entropy_count = sizeof(entropy.buffer) * 8,
772 + .buffer_size = sizeof(entropy.buffer),
773 + .buffer = "Adding real entropy is not actually important for these tests. Don't try this at home, kids!"
774 + };
775 +
776 + if (mknod("/dev/urandom", S_IFCHR | 0644, makedev(1, 9)))
777 + panic("mknod(/dev/urandom)");
778 + fd = open("/dev/urandom", O_WRONLY);
779 + if (fd < 0)
780 + panic("open(urandom)");
781 + for (int i = 0; i < 256; ++i) {
782 + if (ioctl(fd, RNDADDENTROPY, &entropy) < 0)
783 + panic("ioctl(urandom)");
784 + }
785 + close(fd);
786 +}
787 +
788 +static void mount_filesystems(void)
789 +{
790 + pretty_message("[+] Mounting filesystems...");
791 + mkdir("/dev", 0755);
792 + mkdir("/proc", 0755);
793 + mkdir("/sys", 0755);
794 + mkdir("/tmp", 0755);
795 + mkdir("/run", 0755);
796 + mkdir("/var", 0755);
797 + if (mount("none", "/dev", "devtmpfs", 0, NULL))
798 + panic("devtmpfs mount");
799 + if (mount("none", "/proc", "proc", 0, NULL))
800 + panic("procfs mount");
801 + if (mount("none", "/sys", "sysfs", 0, NULL))
802 + panic("sysfs mount");
803 + if (mount("none", "/tmp", "tmpfs", 0, NULL))
804 + panic("tmpfs mount");
805 + if (mount("none", "/run", "tmpfs", 0, NULL))
806 + panic("tmpfs mount");
807 + if (mount("none", "/sys/kernel/debug", "debugfs", 0, NULL))
808 + ; /* Not a problem if it fails.*/
809 + if (symlink("/run", "/var/run"))
810 + panic("run symlink");
811 + if (symlink("/proc/self/fd", "/dev/fd"))
812 + panic("fd symlink");
813 +}
814 +
815 +static void enable_logging(void)
816 +{
817 + int fd;
818 + pretty_message("[+] Enabling logging...");
819 + fd = open("/proc/sys/kernel/printk", O_WRONLY);
820 + if (fd >= 0) {
821 + if (write(fd, "9\n", 2) != 2)
822 + panic("write(printk)");
823 + close(fd);
824 + }
825 + fd = open("/proc/sys/debug/exception-trace", O_WRONLY);
826 + if (fd >= 0) {
827 + if (write(fd, "1\n", 2) != 2)
828 + panic("write(exception-trace)");
829 + close(fd);
830 + }
831 + fd = open("/proc/sys/kernel/panic_on_warn", O_WRONLY);
832 + if (fd >= 0) {
833 + if (write(fd, "1\n", 2) != 2)
834 + panic("write(panic_on_warn)");
835 + close(fd);
836 + }
837 +}
838 +
839 +static void kmod_selftests(void)
840 +{
841 + FILE *file;
842 + char line[2048], *start, *pass;
843 + bool success = true;
844 + pretty_message("[+] Module self-tests:");
845 + file = fopen("/proc/kmsg", "r");
846 + if (!file)
847 + panic("fopen(kmsg)");
848 + if (fcntl(fileno(file), F_SETFL, O_NONBLOCK) < 0)
849 + panic("fcntl(kmsg, nonblock)");
850 + while (fgets(line, sizeof(line), file)) {
851 + start = strstr(line, "wireguard: ");
852 + if (!start)
853 + continue;
854 + start += 11;
855 + *strchrnul(start, '\n') = '\0';
856 + if (strstr(start, "www.wireguard.com"))
857 + break;
858 + pass = strstr(start, ": pass");
859 + if (!pass || pass[6] != '\0') {
860 + success = false;
861 + printf(" \x1b[31m* %s\x1b[0m\n", start);
862 + } else
863 + printf(" \x1b[32m* %s\x1b[0m\n", start);
864 + }
865 + fclose(file);
866 + if (!success) {
867 + puts("\x1b[31m\x1b[1m[-] Tests failed! \u2639\x1b[0m");
868 + poweroff();
869 + }
870 +}
871 +
872 +static void launch_tests(void)
873 +{
874 + char cmdline[4096], *success_dev;
875 + int status, fd;
876 + pid_t pid;
877 +
878 + pretty_message("[+] Launching tests...");
879 + pid = fork();
880 + if (pid == -1)
881 + panic("fork");
882 + else if (pid == 0) {
883 + execl("/init.sh", "init", NULL);
884 + panic("exec");
885 + }
886 + if (waitpid(pid, &status, 0) < 0)
887 + panic("waitpid");
888 + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
889 + pretty_message("[+] Tests successful! :-)");
890 + fd = open("/proc/cmdline", O_RDONLY);
891 + if (fd < 0)
892 + panic("open(/proc/cmdline)");
893 + if (read(fd, cmdline, sizeof(cmdline) - 1) <= 0)
894 + panic("read(/proc/cmdline)");
895 + cmdline[sizeof(cmdline) - 1] = '\0';
896 + for (success_dev = strtok(cmdline, " \n"); success_dev; success_dev = strtok(NULL, " \n")) {
897 + if (strncmp(success_dev, "wg.success=", 11))
898 + continue;
899 + memcpy(success_dev + 11 - 5, "/dev/", 5);
900 + success_dev += 11 - 5;
901 + break;
902 + }
903 + if (!success_dev || !strlen(success_dev))
904 + panic("Unable to find success device");
905 +
906 + fd = open(success_dev, O_WRONLY);
907 + if (fd < 0)
908 + panic("open(success_dev)");
909 + if (write(fd, "success\n", 8) != 8)
910 + panic("write(success_dev)");
911 + close(fd);
912 + } else {
913 + const char *why = "unknown cause";
914 + int what = -1;
915 +
916 + if (WIFEXITED(status)) {
917 + why = "exit code";
918 + what = WEXITSTATUS(status);
919 + } else if (WIFSIGNALED(status)) {
920 + why = "signal";
921 + what = WTERMSIG(status);
922 + }
923 + printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what);
924 + }
925 +}
926 +
927 +static void ensure_console(void)
928 +{
929 + for (unsigned int i = 0; i < 1000; ++i) {
930 + int fd = open("/dev/console", O_RDWR);
931 + if (fd < 0) {
932 + usleep(50000);
933 + continue;
934 + }
935 + dup2(fd, 0);
936 + dup2(fd, 1);
937 + dup2(fd, 2);
938 + close(fd);
939 + if (write(1, "\0\0\0\0\n", 5) == 5)
940 + return;
941 + }
942 + panic("Unable to open console device");
943 +}
944 +
945 +static void clear_leaks(void)
946 +{
947 + int fd;
948 +
949 + fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
950 + if (fd < 0)
951 + return;
952 + pretty_message("[+] Starting memory leak detection...");
953 + write(fd, "clear\n", 5);
954 + close(fd);
955 +}
956 +
957 +static void check_leaks(void)
958 +{
959 + int fd;
960 +
961 + fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
962 + if (fd < 0)
963 + return;
964 + pretty_message("[+] Scanning for memory leaks...");
965 + sleep(2); /* Wait for any grace periods. */
966 + write(fd, "scan\n", 5);
967 + close(fd);
968 +
969 + fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
970 + if (fd < 0)
971 + return;
972 + if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
973 + panic("Memory leaks encountered");
974 + close(fd);
975 +}
976 +
977 +int main(int argc, char *argv[])
978 +{
979 + seed_rng();
980 + ensure_console();
981 + print_banner();
982 + mount_filesystems();
983 + kmod_selftests();
984 + enable_logging();
985 + clear_leaks();
986 + launch_tests();
987 + check_leaks();
988 + poweroff();
989 + return 1;
990 +}
991 --- /dev/null
992 +++ b/tools/testing/selftests/wireguard/qemu/kernel.config
993 @@ -0,0 +1,86 @@
994 +CONFIG_LOCALVERSION=""
995 +CONFIG_NET=y
996 +CONFIG_NETDEVICES=y
997 +CONFIG_NET_CORE=y
998 +CONFIG_NET_IPIP=y
999 +CONFIG_DUMMY=y
1000 +CONFIG_VETH=y
1001 +CONFIG_MULTIUSER=y
1002 +CONFIG_NAMESPACES=y
1003 +CONFIG_NET_NS=y
1004 +CONFIG_UNIX=y
1005 +CONFIG_INET=y
1006 +CONFIG_IPV6=y
1007 +CONFIG_NETFILTER=y
1008 +CONFIG_NETFILTER_ADVANCED=y
1009 +CONFIG_NF_CONNTRACK=y
1010 +CONFIG_NF_NAT=y
1011 +CONFIG_NETFILTER_XTABLES=y
1012 +CONFIG_NETFILTER_XT_NAT=y
1013 +CONFIG_NETFILTER_XT_MATCH_LENGTH=y
1014 +CONFIG_NF_CONNTRACK_IPV4=y
1015 +CONFIG_NF_NAT_IPV4=y
1016 +CONFIG_IP_NF_IPTABLES=y
1017 +CONFIG_IP_NF_FILTER=y
1018 +CONFIG_IP_NF_NAT=y
1019 +CONFIG_IP_ADVANCED_ROUTER=y
1020 +CONFIG_IP_MULTIPLE_TABLES=y
1021 +CONFIG_IPV6_MULTIPLE_TABLES=y
1022 +CONFIG_TTY=y
1023 +CONFIG_BINFMT_ELF=y
1024 +CONFIG_BINFMT_SCRIPT=y
1025 +CONFIG_VDSO=y
1026 +CONFIG_VIRTUALIZATION=y
1027 +CONFIG_HYPERVISOR_GUEST=y
1028 +CONFIG_PARAVIRT=y
1029 +CONFIG_KVM_GUEST=y
1030 +CONFIG_PARAVIRT_SPINLOCKS=y
1031 +CONFIG_PRINTK=y
1032 +CONFIG_KALLSYMS=y
1033 +CONFIG_BUG=y
1034 +CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y
1035 +CONFIG_EMBEDDED=n
1036 +CONFIG_BASE_FULL=y
1037 +CONFIG_FUTEX=y
1038 +CONFIG_SHMEM=y
1039 +CONFIG_SLUB=y
1040 +CONFIG_SPARSEMEM_VMEMMAP=y
1041 +CONFIG_SMP=y
1042 +CONFIG_SCHED_SMT=y
1043 +CONFIG_SCHED_MC=y
1044 +CONFIG_NUMA=y
1045 +CONFIG_PREEMPT=y
1046 +CONFIG_NO_HZ=y
1047 +CONFIG_NO_HZ_IDLE=y
1048 +CONFIG_NO_HZ_FULL=n
1049 +CONFIG_HZ_PERIODIC=n
1050 +CONFIG_HIGH_RES_TIMERS=y
1051 +CONFIG_ARCH_RANDOM=y
1052 +CONFIG_FILE_LOCKING=y
1053 +CONFIG_POSIX_TIMERS=y
1054 +CONFIG_DEVTMPFS=y
1055 +CONFIG_PROC_FS=y
1056 +CONFIG_PROC_SYSCTL=y
1057 +CONFIG_SYSFS=y
1058 +CONFIG_TMPFS=y
1059 +CONFIG_CONSOLE_LOGLEVEL_DEFAULT=15
1060 +CONFIG_PRINTK_TIME=y
1061 +CONFIG_BLK_DEV_INITRD=y
1062 +CONFIG_LEGACY_VSYSCALL_NONE=y
1063 +CONFIG_KERNEL_GZIP=y
1064 +CONFIG_PANIC_ON_OOPS=y
1065 +CONFIG_BUG_ON_DATA_CORRUPTION=y
1066 +CONFIG_LOCKUP_DETECTOR=y
1067 +CONFIG_SOFTLOCKUP_DETECTOR=y
1068 +CONFIG_HARDLOCKUP_DETECTOR=y
1069 +CONFIG_WQ_WATCHDOG=y
1070 +CONFIG_DETECT_HUNG_TASK=y
1071 +CONFIG_BOOTPARAM_HARDLOCKUP_PANIC=y
1072 +CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC=y
1073 +CONFIG_BOOTPARAM_HUNG_TASK_PANIC=y
1074 +CONFIG_PANIC_TIMEOUT=-1
1075 +CONFIG_STACKTRACE=y
1076 +CONFIG_EARLY_PRINTK=y
1077 +CONFIG_GDB_SCRIPTS=y
1078 +CONFIG_WIREGUARD=y
1079 +CONFIG_WIREGUARD_DEBUG=y