forked from multipath-tcp/mptcp-upstream-virtme-docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.sh
executable file
Β·1715 lines (1384 loc) Β· 39.2 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# The goal is to launch (MPTCP) kernel selftests and more.
# But also to provide a dev env for kernel developers or testers.
#
# Arguments:
# - "manual": to have a console in the VM. Additional args are for the kconfig
# - args we pass to kernel's "scripts/config" script.
# We should manage all errors in this script
set -e
is_ci() {
[ "${CI}" = "true" ]
}
is_github_action() {
[ "${GITHUB_ACTIONS}" = "true" ]
}
trace_needed() {
[ "${INPUT_TRACE}" = "1" ]
}
set_trace_on() {
if trace_needed; then
set -x
fi
}
set_trace_off() {
if trace_needed; then
set +x
fi
}
with_clang() {
[ "${INPUT_CLANG}" = "1" ]
}
with_btf() {
[[ "${INPUT_MODE}" == *"btf" ]]
}
set_trace_on
# The behaviour can be changed with 'input' env var
: "${INPUT_CCACHE_MAXSIZE:=5G}"
: "${INPUT_NO_BLOCK:=0}"
: "${INPUT_PACKETDRILL_NO_SYNC:=0}"
: "${INPUT_PACKETDRILL_NO_MORE_TOLERANCE:=0}"
: "${INPUT_PACKETDRILL_STABLE:=0}"
: "${INPUT_RUN_LOOP_CONTINUE:=0}"
: "${INPUT_RUN_TESTS_ONLY:=""}"
: "${INPUT_RUN_TESTS_EXCEPT:=""}"
: "${INPUT_SELFTESTS_DIR:=""}"
: "${INPUT_SELFTESTS_MPTCP_LIB_EXPECT_ALL_FEATURES:=1}"
: "${INPUT_SELFTESTS_MPTCP_LIB_OVERRIDE_FLAKY:=0}"
: "${INPUT_SELFTESTS_MPTCP_LIB_COLOR_FORCE:=1}"
: "${INPUT_CPUS:=""}"
: "${INPUT_CI_RESULTS_DIR:=""}"
: "${INPUT_CI_PRINT_EXIT_CODE:=1}"
: "${INPUT_CI_TIMEOUT_SEC:=7200}"
: "${INPUT_EXPECT_TIMEOUT:="-1"}"
: "${INPUT_BUILD_SKIP_PERF:=1}"
if [ -z "${INPUT_MODE}" ]; then
INPUT_MODE="${1}"
shift || true # none set
fi
# to be able to set an extra env var
if [[ "${INPUT_EXTRA_ENV}" =~ ^"INPUT_"[A-Z0-9_]+"="[a-zA-Z0-9_]+$ ]]; then
eval "${INPUT_EXTRA_ENV}"
fi
: "${PACKETDRILL_GIT_BRANCH:=mptcp-net-next}"
: "${VIRTME_ARCH:="$(uname -m)"}"
TIMESTAMPS_SEC_START=$(date +%s)
# CI only: estimated time before (clone + boot) and after (artifacts) running this script
VIRTME_CI_ESTIMATED_EXTRA_TIME="540"
# max time to boot: it should take less than one minute with a debug kernel, *5 to be safe
VIRTME_EXPECT_BOOT_TIMEOUT="300"
KERNEL_SRC="${PWD}"
# used to pass environment variables to the VM
BASH_PROFILE="/root/.bash_profile"
VIRTME_WORKDIR="${KERNEL_SRC}/.virtme"
VIRTME_BUILD_DIR="${VIRTME_WORKDIR}/build"
with_clang && VIRTME_BUILD_DIR+="-clang"
with_btf && VIRTME_BUILD_DIR+="-btf"
VIRTME_SCRIPTS_DIR="${VIRTME_WORKDIR}/scripts"
VIRTME_HEADERS_DIR="${VIRTME_WORKDIR}/headers"
VIRTME_PERF_DIR="${VIRTME_BUILD_DIR}/tools/perf"
VIRTME_TOOLS_SBIN_DIR="${VIRTME_BUILD_DIR}/tools/sbin"
VIRTME_CACHE_DIR="${VIRTME_BUILD_DIR}/.cache"
VIRTME_KCONFIG="${VIRTME_BUILD_DIR}/.config"
VIRTME_SCRIPT="${VIRTME_SCRIPTS_DIR}/tests.sh"
VIRTME_SCRIPT_END="__VIRTME_END__"
VIRTME_SCRIPT_UNEXPECTED_STOP="Unexpected stop of the VM"
VIRTME_RUN_SCRIPT="${VIRTME_SCRIPTS_DIR}/virtme.sh"
VIRTME_RUN_EXPECT="${VIRTME_SCRIPTS_DIR}/virtme.expect"
SELFTESTS_DIR="${INPUT_SELFTESTS_DIR:-tools/testing/selftests/net/mptcp}"
SELFTESTS_CONFIG="${SELFTESTS_DIR}/config"
BPFTESTS_DIR="${INPUT_BPFTESTS_DIR:-tools/testing/selftests/bpf}"
BPFTESTS_CONFIG="${BPFTESTS_DIR}/config"
export CCACHE_MAXSIZE="${INPUT_CCACHE_MAXSIZE}"
export CCACHE_DIR="${VIRTME_WORKDIR}/ccache"
with_clang && CCACHE_DIR+="-clang"
with_btf && CCACHE_DIR+="-btf"
export KBUILD_OUTPUT="${VIRTME_BUILD_DIR}"
export KCONFIG_CONFIG="${VIRTME_KCONFIG}"
mkdir -p \
"${VIRTME_BUILD_DIR}" \
"${VIRTME_SCRIPTS_DIR}" \
"${VIRTME_HEADERS_DIR}" \
"${VIRTME_PERF_DIR}" \
"${VIRTME_CACHE_DIR}" \
"${CCACHE_DIR}"
chmod 777 "${VIRTME_CACHE_DIR}" # to let users writting files there, e.g. clangd
VIRTME_CONFIGKERNEL="virtme-configkernel"
VIRTME_RUN="virtme-run"
VIRTME_RUN_OPTS_DEFAULT=(
--arch "${VIRTME_ARCH}"
--name "mptcpdev" # hostname
--memory 2048M
--kdir "${VIRTME_BUILD_DIR}"
--mods=auto
--rw # Don't use "rwdir", it will use 9p ; in a container, we can use rw
--pwd
--show-command
--verbose --show-boot-console
--kopt mitigations=off
)
# results dir
RESULTS_DIR_BASE="${VIRTME_WORKDIR}/results"
RESULTS_DIR=
# log files
OUTPUT_VIRTME=
TESTS_SUMMARY=
CONCLUSION=
KMEMLEAK=
EXIT_STATUS=0
EXIT_REASONS=()
EXIT_TITLE="KVM Validation"
EXPECT=0
VIRTME_EXEC_RUN="${KERNEL_SRC}/.virtme-exec-run"
VIRTME_EXEC_PRE="${KERNEL_SRC}/.virtme-exec-pre"
VIRTME_EXEC_POST="${KERNEL_SRC}/.virtme-exec-post"
VIRTME_PREPARE_POST="${KERNEL_SRC}/.virtme-prepare-post"
COLOR_RED="\E[1;31m"
COLOR_GREEN="\E[1;32m"
COLOR_YELLOW="\E[1;33m"
COLOR_BLUE="\E[1;34m"
COLOR_RESET="\E[0m"
# $1: color, $2: text
print_color() {
echo -e "${START_PRINT:-}${*}${COLOR_RESET}"
}
print() {
print_color "${COLOR_GREEN}${*}"
}
printinfo() {
print_color "${COLOR_BLUE}${*}"
}
printerr() {
print_color "${COLOR_RED}${*}" >&2
}
if is_github_action; then
# $1: description
log_section_start() {
echo -e "\n::group::${COLOR_YELLOW}${*}${COLOR_RESET}"
}
log_section_end() {
echo -e "::endgroup::\n"
}
else
# $1: description
log_section_start() {
printinfo "${@}"
}
log_section_end() {
true
}
fi
setup_env() { local mode net=()
mode="${1}"
log_section_start "Setup environment"
# Avoid 'unsafe repository' error: we need to get the rev/tag later from
# this docker image
git config --global --replace-all safe.directory "${KERNEL_SRC}" || true
# Set a name, just in case for automations
git config --global user.name "MPTCP Virtme Docker"
git config --global user.email "[email protected]"
if is_ci; then
# Root dir: not to have to go down dirs to get artifacts
RESULTS_DIR="${KERNEL_SRC}${INPUT_CI_RESULTS_DIR:+/${INPUT_CI_RESULTS_DIR}}"
mkdir -p "${RESULTS_DIR}"
: "${INPUT_CPUS:=$(nproc)}" # use all available resources
EXIT_TITLE="${EXIT_TITLE}: ${mode}" # only one mode
if [ -n "${INPUT_RUN_TESTS_ONLY}" ]; then
EXIT_TITLE="${EXIT_TITLE} (only ${INPUT_RUN_TESTS_ONLY})"
fi
if [ -n "${INPUT_RUN_TESTS_EXCEPT}" ]; then
EXIT_TITLE="${EXIT_TITLE} (except ${INPUT_RUN_TESTS_EXCEPT})"
fi
# The CI doesn't need to access to the outside world, so no '--net'
else
# avoid override
RESULTS_DIR="${RESULTS_DIR_BASE}/$(git rev-parse --short HEAD || echo "UNKNOWN")/${mode}"
rm -rf "${RESULTS_DIR}"
mkdir -p "${RESULTS_DIR}"
: "${INPUT_CPUS:=2}" # limit to 2 cores for now
# add net support, can be useful, but delay the start of the tests (~1 sec?)
net=("--net")
fi
VIRTME_RUN_OPTS=(
"${VIRTME_RUN_OPTS_DEFAULT[@]}"
--cpus "${INPUT_CPUS}"
"${net[@]}"
)
OUTPUT_VIRTME="${RESULTS_DIR}/output.log"
TESTS_SUMMARY="${RESULTS_DIR}/summary.txt"
CONCLUSION="${RESULTS_DIR}/conclusion.txt"
KMEMLEAK="${RESULTS_DIR}/kmemleak.txt"
KVERSION=$(make -C "${KERNEL_SRC}" -s kernelversion) ## 5.17.0 or 5.17.0-rc8
KVER_MAJ=${KVERSION%%.*} ## 5
KVER_MIN=${KVERSION#*.} ## 17.0*
KVER_MIC=${KVER_MIN#*.} ## 0
KVER_MIN=${KVER_MIN%%.*} ## 17
# without rc, it means we probably already merged with net-next
if [[ ! "${KVERSION}" =~ rc ]] && [ "${KVER_MIC}" = 0 ]; then
KVER_MIN=$((KVER_MIN + 1))
# max .19 because Linus has 20 fingers
if [ ${KVER_MIN} -gt 19 ]; then
KVER_MAJ=$((KVER_MAJ + 1))
KVER_MIN=0
fi
fi
if [ "${KVER_MAJ}" -lt 5 ] ||
{ [ "${KVER_MAJ}" -eq 5 ] && [ "${KVER_MIN}" -le 10 ]; }; then
# virtiofs doesn't seem to be supported on old kernels
VIRTME_RUN_OPTS+=(--force-9p)
fi
log_section_end
}
_get_last_iproute_version() {
curl https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/refs/tags 2>/dev/null | \
grep "/tag/?h=v[0-9]" | \
cut -d\' -f2 | cut -d= -f2 | \
sort -Vu | \
tail -n1
}
check_last_iproute() { local last curr
# only check on CI
if ! is_ci; then
return 0
fi
# skip the check for stable, fine not to have the latest version
if [ "${INPUT_PACKETDRILL_STABLE}" = "1" ]; then
return 0
fi
log_section_start "Check IPRoute2 version"
last="$(_get_last_iproute_version)"
curr="v$(ip -V | sed 's/.*iproute2-\([0-9.]\+\).*/\1/')"
if [ "${curr}" = "${last}" ]; then
printinfo "IPRoute2: using the last version: ${last}"
else
printerr "WARN: IPRoute2: not the last version: ${curr} < ${last}"
fi
log_section_end
}
_check_source_exec_one() {
local src="${1}"
local reason="${2}"
if [ -f "${src}" ]; then
printinfo "This script file exists and will be used ${reason}: $(basename "${src}")"
cat -n "${src}"
if is_ci || [ "${INPUT_NO_BLOCK}" = "1" ]; then
printinfo "Check source exec: not blocking"
else
print "Press Enter to continue (use 'INPUT_NO_BLOCK=1' to avoid this)"
read -r
fi
fi
}
check_source_exec_all() {
log_section_start "Check extented exec files"
_check_source_exec_one "${VIRTME_EXEC_PRE}" "before the tests suite"
_check_source_exec_one "${VIRTME_EXEC_RUN}" "to replace the execution of the whole tests suite"
_check_source_exec_one "${VIRTME_EXEC_POST}" "after the tests suite"
log_section_end
}
read -ra MAKE_ARGS <<< "${INPUT_MAKE_ARGS}"
with_clang && MAKE_ARGS+=(LLVM=1 LLVM_IAS=1 CC=clang ARCH="${VIRTME_ARCH}")
MAKE_ARGS_O=("${MAKE_ARGS[@]}" O="${VIRTME_BUILD_DIR}")
_make_j() {
make -j"$(nproc)" -l"$(nproc)" "${@}"
}
_make() {
_make_j "${MAKE_ARGS[@]}" "${@}"
}
_make_o() {
_make_j "${MAKE_ARGS_O[@]}" "${@}"
}
# $1: source ; $2: target
_add_symlink() {
local src="${1}"
local dst="${2}"
if [ -e "${dst}" ] && [ ! -L "${dst}" ]; then
printerr "${dst} already exists and is not a symlink, please remove it"
return 1
fi
ln -sf "${src}" "${dst}"
}
# $1: mode ; [rest: extra kconfig]
gen_kconfig() { local mode kconfig=() vck rc=0
mode="${1}"
shift
log_section_start "Generate kernel config"
vck=(--arch "${VIRTME_ARCH}" --defconfig --custom "${SELFTESTS_CONFIG}")
if [ "${mode}" = "debug" ]; then
kconfig+=(
-e NET_NS_REFCNT_TRACKER # useful for 'net' tests
-d SLUB_DEBUG_ON # perf impact is too important
-e BOOTPARAM_SOFTLOCKUP_PANIC # instead of blocking
-e BOOTPARAM_HUNG_TASK_PANIC # instead of blocking
)
local debug_config="kernel/configs/debug.config"
# Introduced in v5.17
if [ ! -s "${debug_config}" ]; then
debug_config="${VIRTME_CACHE_DIR}/debug.config"
curl "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/kernel/configs/debug.config" > "${debug_config}"
fi
vck+=(--custom "${debug_config}")
else
# low-overhead sampling-based memory safety error detector.
# Only in non-debug: KASAN is more precise
kconfig+=(-e KFENCE)
fi
# stop at the first oops, no need to continue in a bad state
kconfig+=(-e PANIC_ON_OOPS)
# Debug info for developers
kconfig+=(-e DEBUG_INFO -e DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT -e GDB_SCRIPTS)
if with_clang; then
kconfig+=(-e DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT)
else
# decode_stacktrace.sh script reports '??:?' with GCC and DWARF5
kconfig+=(-e DEBUG_INFO_DWARF4)
fi
# Compressed (old/new option)
kconfig+=(-e DEBUG_INFO_COMPRESSED -e DEBUG_INFO_COMPRESSED_ZLIB)
# We need more debug info but it is slow to generate
if [ "${mode}" = "btf" ]; then
vck+=(--custom "${BPFTESTS_CONFIG}")
kconfig+=(-e DEBUG_INFO_BTF_MODULES -e MODULE_ALLOW_BTF_MISMATCH)
# Fix ./include/linux/if.h:28:10: fatal error:
# sys/socket.h: no such file or directory
kconfig+=(-d IA32_EMULATION)
elif is_ci || [ "${mode}" != "debsym" ]; then
kconfig+=(-e DEBUG_INFO_REDUCED -e DEBUG_INFO_SPLIT)
fi
# Debug tools for developers
kconfig+=(
-e DYNAMIC_DEBUG --set-val CONSOLE_LOGLEVEL_DEFAULT 8
-e FTRACE -e FUNCTION_TRACER -e DYNAMIC_FTRACE
-e FTRACE_SYSCALLS -e HIST_TRIGGERS
)
# Extra sanity checks in networking: for the moment, small checks
kconfig+=(-e DEBUG_NET)
# Extra options needed for MPTCP KUnit tests
kconfig+=(-m KUNIT -e KUNIT_DEBUGFS -d KUNIT_ALL_TESTS -m MPTCP_KUNIT_TEST)
# Options for BPF
kconfig+=(-e BPF_JIT -e BPF_SYSCALL)
# Extra options needed for packetdrill
# note: we still need SHA1 for fallback tests with v0
kconfig+=(-e TUN -e CRYPTO_USER_API_HASH -e CRYPTO_SHA1)
# Useful to reproduce issue
kconfig+=(-e NET_SCH_TBF)
# Disable retpoline to accelerate tests
kconfig+=(-d RETPOLINE)
# Disable components we don't need
kconfig+=(
-d PCCARD -d MACINTOSH_DRIVERS -d SOUND -d USB_SUPPORT
-d NEW_LEDS -d SURFACE_PLATFORMS -d DRM -d FB
)
# extra config
kconfig+=("${@}")
# KBUILD_OUTPUT is used by virtme
"${VIRTME_CONFIGKERNEL}" "${vck[@]}" "${MAKE_ARGS_O[@]}" || rc=${?}
./scripts/config --file "${VIRTME_KCONFIG}" "${kconfig[@]}" || rc=${?}
_make_o olddefconfig || rc=${?}
if is_ci; then
# Useful to help reproducing issues later
zstd -19 -T0 "${VIRTME_KCONFIG}" -o "${RESULTS_DIR}/config.zstd" || rc=${?}
fi
log_section_end
return ${rc}
}
build_kernel() { local rc=0
log_section_start "Build kernel"
_make_o || rc=${?}
# virtme will mount a tmpfs there + symlink to .virtme_mods
mkdir -p /lib/modules
log_section_end
return ${rc}
}
build_compile_commands() { local rc=0
log_section_start "Build compile_commands.json"
_make_o compile_commands.json || rc=${?}
log_section_end
return ${rc}
}
install_kernel_headers() { local rc=0
log_section_start "Install kernel headers"
# for BPFTrace and cie
cp -r include/ "${VIRTME_BUILD_DIR}"
_make_o headers_install INSTALL_HDR_PATH="${VIRTME_HEADERS_DIR}" || rc=${?}
log_section_end
return ${rc}
}
build_perf() { local rc=0
if [ "${INPUT_BUILD_SKIP_PERF}" = 1 ]; then
printinfo "Skip Perf build"
return 0
fi
log_section_start "Build Perf"
cd tools/perf
_make O="${VIRTME_PERF_DIR}" DESTDIR=/usr install || rc=${?}
cd "${KERNEL_SRC}"
log_section_end
return ${rc}
}
build() {
if [ "${INPUT_BUILD_SKIP}" = 1 ]; then
printinfo "Skip kernel build"
return 0
fi
build_kernel
if [ "${EXPECT}" = 0 ] && with_clang; then
build_compile_commands || true # nice to have
fi
install_kernel_headers
build_perf
}
build_selftests() { local rc=0
if [ "${INPUT_BUILD_SKIP_SELFTESTS}" = 1 ]; then
printinfo "Skip selftests build"
return 0
fi
log_section_start "Build the selftests $(basename "${SELFTESTS_DIR}")"
_make_o KHDR_INCLUDES="-I${VIRTME_HEADERS_DIR}/include" -C "${SELFTESTS_DIR}" || rc=${?}
log_section_end
return ${rc}
}
build_bpftests() { local rc=0
if [ "${INPUT_BUILD_SKIP_BPFTESTS}" = 1 ]; then
printinfo "Skip bpftests build"
return 0
fi
log_section_start "Build BPFTests"
_make_o KHDR_INCLUDES="-I${VIRTME_HEADERS_DIR}/include" -C "${BPFTESTS_DIR}" || rc=${?}
log_section_end
return ${rc}
}
build_packetdrill() { local old_pwd kversion branch rc=0
if [ "${INPUT_BUILD_SKIP_PACKETDRILL}" = 1 ]; then
printinfo "Skip Packetdrill build"
return 0
fi
log_section_start "Build Packetdrill"
old_pwd="${PWD}"
# make sure we have the last stable tests
cd /opt/packetdrill/
if [ "${INPUT_PACKETDRILL_NO_SYNC}" = "1" ]; then
printinfo "Packetdrill: no sync"
else
git fetch origin
branch="${PACKETDRILL_GIT_BRANCH}"
if [ "${INPUT_PACKETDRILL_STABLE}" = "1" ]; then
kversion="mptcp-${KVER_MAJ}.${KVER_MIN}"
# set the new branch only if it exists. If not, take the dev one
if git show-ref --quiet "refs/remotes/origin/${kversion}"; then
branch="${kversion}"
fi
fi
git checkout -f "origin/${branch}"
fi
cd gtests/net/packetdrill/
./configure
_make || rc=${?}
if [ "${INPUT_PACKETDRILL_NO_MORE_TOLERANCE}" = "1" ]; then
printinfo "Packetdrill: not modifying the tolerance"
else
cd ../mptcp
# reduce debug logs: too much
set_trace_off
local pf val new_val
for pf in $(git grep -l "^--tolerance_usecs="); do
# shellcheck disable=SC2013 # to filter duplicated ones
for val in $(grep "^--tolerance_usecs=" "${pf}" | cut -d= -f2 | sort -u); do
if [ "${mode}" = "debug" ]; then
# Increase tolerance in debug mode:
# the environment can be very slow
new_val=$((val * 8))
else
# Triple the time in normal mode:
# public CI can be quite loaded...
new_val=$((val * 3))
fi
sed -i "s/^--tolerance_usecs=${val}$/--tolerance_usecs=${new_val}/g" "${pf}"
done
done
set_trace_on
fi
cd "${old_pwd}"
log_section_end
return ${rc}
}
prepare() { local mode no_tap=1
mode="${1}"
printinfo "Prepare the environment"
build_selftests
if [ "${mode}" = "btf" ]; then
build_bpftests
fi
build_packetdrill
if is_ci; then
no_tap=0 # we want subtests
fi
cat <<EOF > "${BASH_PROFILE}"
export KERNEL_BUILD_DIR="${VIRTME_BUILD_DIR}"
export KERNEL_SRC_DIR="${KERNEL_SRC}"
export PATH="\${PATH}:${VIRTME_TOOLS_SBIN_DIR}"
EOF
cat <<EOF > "${VIRTME_SCRIPT}"
#! /bin/bash
if [ "${INPUT_TRACE}" = "1" ]; then
set -x
fi
# useful for virtme-exec-run
TAP_PREFIX="${KERNEL_SRC}/tools/testing/selftests/kselftest/prefix.pl"
RESULTS_DIR="${RESULTS_DIR}"
OUTPUT_VIRTME="${OUTPUT_VIRTME}"
KUNIT_CORE_LOADED=0
MAX_THREADS=${INPUT_MAX_THREADS:-$((INPUT_CPUS * 2))}
export SELFTESTS_MPTCP_LIB_EXPECT_ALL_FEATURES="${INPUT_SELFTESTS_MPTCP_LIB_EXPECT_ALL_FEATURES}"
export SELFTESTS_MPTCP_LIB_OVERRIDE_FLAKY="${INPUT_SELFTESTS_MPTCP_LIB_OVERRIDE_FLAKY}"
export SELFTESTS_MPTCP_LIB_COLOR_FORCE="${INPUT_SELFTESTS_MPTCP_LIB_COLOR_FORCE}"
export SELFTESTS_MPTCP_LIB_NO_TAP="${no_tap}"
set_max_threads() {
# if QEmu without KVM support
if [ "${mode}" == "debug" ] ||
{ [ "\$(cat /sys/devices/virtual/dmi/id/sys_vendor)" = "QEMU" ] &&
[ "\$(cat /sys/devices/system/clocksource/clocksource0/current_clocksource)" != "kvm-clock" ]; }; then
MAX_THREADS=$((MAX_THREADS / 2)) # avoid too many concurrent work
fi
}
if [ "${GITHUB_ACTIONS}" = "true" ]; then
# \$1: description
log_section_start() {
echo -e "\n::group::${COLOR_YELLOW}\${*}${COLOR_RESET}"
}
log_section_end() {
echo -e "::endgroup::\n"
}
else
# \$1: description
log_section_start() {
echo -e "${COLOR_BLUE}\${*}${COLOR_RESET}"
}
log_section_end() {
true
}
fi
# \$1: name of the test
_can_run() { local tname
tname="\${1}"
# only some tests?
if [ -n "${INPUT_RUN_TESTS_ONLY}" ]; then
if ! echo "${INPUT_RUN_TESTS_ONLY}" | grep -wq "\${tname}"; then
return 1
fi
fi
# not some tests?
if [ -n "${INPUT_RUN_TESTS_EXCEPT}" ]; then
if echo "${INPUT_RUN_TESTS_EXCEPT}" | grep -wq "\${tname}"; then
return 1
fi
fi
return 0
}
can_run() {
# Use the function name of the caller without the prefix
_can_run "\${FUNCNAME[1]#*_}"
}
# \$1: file ; \$2+: commands
_tap() { local out out_subtests tmp fname rc ok nok msg cmt ts_s_start ts_s_stop
out="\${1}.tap"
out_subtests="\${1}_subtests.tap"
fname="\$(basename \${1})"
shift
rm -f "\${out}" "\${out_subtests}"
# With TAP, we have first the summary, then the diagnostic
tmp="\${out}.tmp"
ok="ok 1 test: \${fname}"
nok="not \${ok}"
ts_s_start=\$(date +%s)
# init
{
echo "TAP version 13"
echo "1..1"
} | tee "\${out}"
# Exec the command and pipe in tap prefix + store for later
"\${@}" 2>&1 | "\${TAP_PREFIX}" | tee "\${tmp}"
# output to stdout now to see the progression
rc=\${PIPESTATUS[0]}
ts_s_stop=\$(date +%s)
# summary
case \${rc} in
0)
msg="\${ok}"
;;
1)
msg="\${nok}"
cmt=FAIL
;;
2)
msg="\${nok}"
cmt=XFAIL
;;
3)
msg="\${nok}"
cmt=XPASS
;;
4)
cmt=SKIP
if [ "\${SELFTESTS_MPTCP_LIB_EXPECT_ALL_FEATURES}" = "1" ]; then
msg="\${nok}"
else
msg="\${ok}"
fi
;;
*)
msg="\${nok}"
cmt="FAIL # exit=\${rc}"
;;
esac
# note: '#' is a directive, not a comment: we imitate kselftest/runner.sh
echo "\${msg}\${cmt+ # }\${cmt}" | tee -a "\${out}"
# diagnostic at the end with TAP
# Strip colours: https://stackoverflow.com/a/18000433
# Also extract subtests displayed at the end, if any, in a different file without "#"
sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g" "\${tmp}" | \
awk "BEGIN { subtests=0 } {
if (subtests == 0 && \\\$0 ~ /^# TAP version /) { subtests=1 };
if (subtests == 0) { print >> \"\${out}\" }
else { for (i=2; i <= NF; i++) printf(\"%s\", ((i>2) ? OFS : \"\") \\\$i) >> \"\${out_subtests}\" ; printf(\"\n\") >> \"\${out_subtests}\" }
}"
rm -f "\${tmp}"
echo "# time=\$((ts_s_stop - ts_s_start))" | tee -a "\${out}"
return \${rc}
}
# \$1: kunit path ; \$2: kunit test
_kunit_result() {
if ! grep -q "^KTAP" "\${1}" 2>/dev/null; then
echo "TAP version 14"
echo "1..1"
fi
if ! cat "\${1}"; then
echo "not ok 1 test: \${2/*//} # no kunit result"
return 1
fi
}
run_kunit_core() {
[ "\${KUNIT_CORE_LOADED}" = 1 ] && return 0
KUNIT_CORE_LOADED=1
_tap "${RESULTS_DIR}/kunit" insmod ${VIRTME_BUILD_DIR}/lib/kunit/kunit.ko
}
# \$1: .ko path
run_kunit_one() { local ko kunit kunit_path rc=0
ko="\${1}"
kunit="\${ko#${VIRTME_BUILD_DIR}/}" # remove abs dir
kunit="\${kunit:10:-8}" # remove net/mptcp (10) + _test.ko (8)
kunit="\${kunit//_/-}" # dash
kunit_path="/sys/kernel/debug/kunit/\${kunit}/results"
log_section_start "KUnit Test: \${kunit}"
run_kunit_core || return \${?}
insmod "\${ko}" || true # errors will also be visible below: no results
_kunit_result "\${kunit_path}" "\${kunit}" | tee "${RESULTS_DIR}/kunit_\${kunit}.tap" || rc=\${?}
log_section_end
return \${rc}
}
run_kunit_all() { local ko rc=0
can_run || return 0
cd "${KERNEL_SRC}"
for ko in ${VIRTME_BUILD_DIR}/net/mptcp/*_test.ko; do
run_kunit_one "\${ko}" || rc=\${?}
done
return \${rc}
}
# \$1: output tap file; rest: command to launch
_run_selftest_one_tap() {
cd "${KERNEL_SRC}/${SELFTESTS_DIR}"
_tap "\${@}"
}
# \$1: script file; rest: command to launch
run_selftest_one() { local sf tap rc=0
sf=\$(basename \${1})
tap=selftest_\${sf:0:-3}
shift
_can_run "\${tap}" || return 0
log_section_start "Selftest Test: ./\${sf}\${1:+ \${*}}"
_run_selftest_one_tap "${RESULTS_DIR}/\${tap}" "./\${sf}" "\${@}" || rc=\${?}
log_section_end
return \${rc}
}
run_selftest_all() { local sf rc=0
# The following command re-do a slow headers install + compilation in a different dir
#make O="${VIRTME_BUILD_DIR}" --silent -C tools/testing/selftests TARGETS=net/mptcp run_tests
for sf in "${KERNEL_SRC}/${SELFTESTS_DIR}/"*.sh; do
if [ -x "\${sf}" ]; then
run_selftest_one "\${sf}" || rc=\${?}
fi
done
return \${rc}
}
_run_mptcp_connect_opt() { local t="mptcp_connect_\${1}" rc=0
shift
log_section_start "Selftest Test: ./mptcp_connect.sh \${*}"
MPTCP_LIB_KSFT_TEST=\${t} _run_selftest_one_tap "${RESULTS_DIR}/\${t}" ./mptcp_connect.sh "\${@}" || rc=\${?}
log_section_end
return \${rc}
}
run_mptcp_connect_mmap() {
can_run || return 0
_run_mptcp_connect_opt mmap -m mmap
}
# \$1: packetdrill TAP file, \$2: TAP prefix
_packetdrill_result() {
if grep -q "^TAP version 13" "\${1}" 2>/dev/null; then
sed -i "s#\${PWD}/#packetdrill: #g" "\${1}" # remove long path + prefix
return 0
fi
{
echo "TAP version 13"
echo "1..1"
echo "not ok 1 test: \${2} # no result"
} > "\${1}"
return 1
}
# \$1: pktd_dir (e.g. mptcp/dss)
run_packetdrill_one() { local pktd_dir pktd tap rc=0
pktd_dir="\${1}"
pktd="\$(basename "\${pktd_dir}" ".pkt")" # remove ext just in case
tap="packetdrill_\${pktd}"
if [ "\${pktd}" = "common" ]; then
return 0
fi
_can_run "\${tap}" || return 0
log_section_start "Packetdrill Test: \${pktd}"
cd /opt/packetdrill/gtests/net/
PYTHONUNBUFFERED=1 ./packetdrill/run_all.py -t "${RESULTS_DIR}" \
-l -v -P \${MAX_THREADS} \${pktd_dir} || rc=\${?}
_packetdrill_result "${RESULTS_DIR}/\${tap}.tap" "\${tap}" || rc=\${?}
log_section_end
return \${rc}
}
run_packetdrill_all() { local pktd_dir rc=0
cd /opt/packetdrill/gtests/net/
# dry run just to "heat" up the environment: the first tests are always
# slower, especially with a debug kernel
./packetdrill/run_all.py mptcp/add_addr/add_addr4_server.pkt &>/dev/null || true
for pktd_dir in mptcp/*; do
run_packetdrill_one "\${pktd_dir}" || rc=\${?}
done
return \${rc}
}
# \$1: output tap file; rest: command to launch
_run_bpftest_one_tap() {
cd "${VIRTME_BUILD_DIR}"
_tap "\${@}"
}
# \$1: script file; rest: command to launch
run_bpftest_one() { local bf bt tap rc=0
bf=\$(basename \${1})
bt=\${2}
tap=bpftest_\${bf}_\${bt}
log_section_start "BPF Test: \${bf} -t \${bt}"
_run_bpftest_one_tap "${RESULTS_DIR}/\${tap}" "./\${bf}" -t "\${bt}" || rc=\${?}
log_section_end
return \${rc}
}
run_bpftest_all() {
can_run || return 0
if [ "${mode}" = "btf" ]; then
local sf rc=0