xref: /linux/tools/testing/selftests/net/mptcp/mptcp_lib.sh (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
1#! /bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4readonly KSFT_FAIL=1
5readonly KSFT_SKIP=4
6
7# SELFTESTS_MPTCP_LIB_EXPECT_ALL_FEATURES env var can be set when validating all
8# features using the last version of the kernel and the selftests to make sure
9# a test is not being skipped by mistake.
10mptcp_lib_expect_all_features() {
11	[ "${SELFTESTS_MPTCP_LIB_EXPECT_ALL_FEATURES:-}" = "1" ]
12}
13
14# $1: msg
15mptcp_lib_fail_if_expected_feature() {
16	if mptcp_lib_expect_all_features; then
17		echo "ERROR: missing feature: ${*}"
18		exit ${KSFT_FAIL}
19	fi
20
21	return 1
22}
23
24# $1: file
25mptcp_lib_has_file() {
26	local f="${1}"
27
28	if [ -f "${f}" ]; then
29		return 0
30	fi
31
32	mptcp_lib_fail_if_expected_feature "${f} file not found"
33}
34
35mptcp_lib_check_mptcp() {
36	if ! mptcp_lib_has_file "/proc/sys/net/mptcp/enabled"; then
37		echo "SKIP: MPTCP support is not available"
38		exit ${KSFT_SKIP}
39	fi
40}
41
42mptcp_lib_check_kallsyms() {
43	if ! mptcp_lib_has_file "/proc/kallsyms"; then
44		echo "SKIP: CONFIG_KALLSYMS is missing"
45		exit ${KSFT_SKIP}
46	fi
47}
48
49# Internal: use mptcp_lib_kallsyms_has() instead
50__mptcp_lib_kallsyms_has() {
51	local sym="${1}"
52
53	mptcp_lib_check_kallsyms
54
55	grep -q " ${sym}" /proc/kallsyms
56}
57
58# $1: part of a symbol to look at, add '$' at the end for full name
59mptcp_lib_kallsyms_has() {
60	local sym="${1}"
61
62	if __mptcp_lib_kallsyms_has "${sym}"; then
63		return 0
64	fi
65
66	mptcp_lib_fail_if_expected_feature "${sym} symbol not found"
67}
68
69# $1: part of a symbol to look at, add '$' at the end for full name
70mptcp_lib_kallsyms_doesnt_have() {
71	local sym="${1}"
72
73	if ! __mptcp_lib_kallsyms_has "${sym}"; then
74		return 0
75	fi
76
77	mptcp_lib_fail_if_expected_feature "${sym} symbol has been found"
78}
79
80# !!!AVOID USING THIS!!!
81# Features might not land in the expected version and features can be backported
82#
83# $1: kernel version, e.g. 6.3
84mptcp_lib_kversion_ge() {
85	local exp_maj="${1%.*}"
86	local exp_min="${1#*.}"
87	local v maj min
88
89	# If the kernel has backported features, set this env var to 1:
90	if [ "${SELFTESTS_MPTCP_LIB_NO_KVERSION_CHECK:-}" = "1" ]; then
91		return 0
92	fi
93
94	v=$(uname -r | cut -d'.' -f1,2)
95	maj=${v%.*}
96	min=${v#*.}
97
98	if   [ "${maj}" -gt "${exp_maj}" ] ||
99	   { [ "${maj}" -eq "${exp_maj}" ] && [ "${min}" -ge "${exp_min}" ]; }; then
100		return 0
101	fi
102
103	mptcp_lib_fail_if_expected_feature "kernel version ${1} lower than ${v}"
104}
105