18c2ecf20Sopenharmony_ci#! /bin/bash
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_cireadonly KSFT_FAIL=1
58c2ecf20Sopenharmony_cireadonly KSFT_SKIP=4
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci# SELFTESTS_MPTCP_LIB_EXPECT_ALL_FEATURES env var can be set when validating all
88c2ecf20Sopenharmony_ci# features using the last version of the kernel and the selftests to make sure
98c2ecf20Sopenharmony_ci# a test is not being skipped by mistake.
108c2ecf20Sopenharmony_cimptcp_lib_expect_all_features() {
118c2ecf20Sopenharmony_ci	[ "${SELFTESTS_MPTCP_LIB_EXPECT_ALL_FEATURES:-}" = "1" ]
128c2ecf20Sopenharmony_ci}
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci# $1: msg
158c2ecf20Sopenharmony_cimptcp_lib_fail_if_expected_feature() {
168c2ecf20Sopenharmony_ci	if mptcp_lib_expect_all_features; then
178c2ecf20Sopenharmony_ci		echo "ERROR: missing feature: ${*}"
188c2ecf20Sopenharmony_ci		exit ${KSFT_FAIL}
198c2ecf20Sopenharmony_ci	fi
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	return 1
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci# $1: file
258c2ecf20Sopenharmony_cimptcp_lib_has_file() {
268c2ecf20Sopenharmony_ci	local f="${1}"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if [ -f "${f}" ]; then
298c2ecf20Sopenharmony_ci		return 0
308c2ecf20Sopenharmony_ci	fi
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	mptcp_lib_fail_if_expected_feature "${f} file not found"
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cimptcp_lib_check_mptcp() {
368c2ecf20Sopenharmony_ci	if ! mptcp_lib_has_file "/proc/sys/net/mptcp/enabled"; then
378c2ecf20Sopenharmony_ci		echo "SKIP: MPTCP support is not available"
388c2ecf20Sopenharmony_ci		exit ${KSFT_SKIP}
398c2ecf20Sopenharmony_ci	fi
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cimptcp_lib_check_kallsyms() {
438c2ecf20Sopenharmony_ci	if ! mptcp_lib_has_file "/proc/kallsyms"; then
448c2ecf20Sopenharmony_ci		echo "SKIP: CONFIG_KALLSYMS is missing"
458c2ecf20Sopenharmony_ci		exit ${KSFT_SKIP}
468c2ecf20Sopenharmony_ci	fi
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci# Internal: use mptcp_lib_kallsyms_has() instead
508c2ecf20Sopenharmony_ci__mptcp_lib_kallsyms_has() {
518c2ecf20Sopenharmony_ci	local sym="${1}"
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	mptcp_lib_check_kallsyms
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	grep -q " ${sym}" /proc/kallsyms
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci# $1: part of a symbol to look at, add '$' at the end for full name
598c2ecf20Sopenharmony_cimptcp_lib_kallsyms_has() {
608c2ecf20Sopenharmony_ci	local sym="${1}"
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	if __mptcp_lib_kallsyms_has "${sym}"; then
638c2ecf20Sopenharmony_ci		return 0
648c2ecf20Sopenharmony_ci	fi
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	mptcp_lib_fail_if_expected_feature "${sym} symbol not found"
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci# $1: part of a symbol to look at, add '$' at the end for full name
708c2ecf20Sopenharmony_cimptcp_lib_kallsyms_doesnt_have() {
718c2ecf20Sopenharmony_ci	local sym="${1}"
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if ! __mptcp_lib_kallsyms_has "${sym}"; then
748c2ecf20Sopenharmony_ci		return 0
758c2ecf20Sopenharmony_ci	fi
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	mptcp_lib_fail_if_expected_feature "${sym} symbol has been found"
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci# !!!AVOID USING THIS!!!
818c2ecf20Sopenharmony_ci# Features might not land in the expected version and features can be backported
828c2ecf20Sopenharmony_ci#
838c2ecf20Sopenharmony_ci# $1: kernel version, e.g. 6.3
848c2ecf20Sopenharmony_cimptcp_lib_kversion_ge() {
858c2ecf20Sopenharmony_ci	local exp_maj="${1%.*}"
868c2ecf20Sopenharmony_ci	local exp_min="${1#*.}"
878c2ecf20Sopenharmony_ci	local v maj min
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	# If the kernel has backported features, set this env var to 1:
908c2ecf20Sopenharmony_ci	if [ "${SELFTESTS_MPTCP_LIB_NO_KVERSION_CHECK:-}" = "1" ]; then
918c2ecf20Sopenharmony_ci		return 0
928c2ecf20Sopenharmony_ci	fi
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	v=$(uname -r | cut -d'.' -f1,2)
958c2ecf20Sopenharmony_ci	maj=${v%.*}
968c2ecf20Sopenharmony_ci	min=${v#*.}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if   [ "${maj}" -gt "${exp_maj}" ] ||
998c2ecf20Sopenharmony_ci	   { [ "${maj}" -eq "${exp_maj}" ] && [ "${min}" -ge "${exp_min}" ]; }; then
1008c2ecf20Sopenharmony_ci		return 0
1018c2ecf20Sopenharmony_ci	fi
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	mptcp_lib_fail_if_expected_feature "kernel version ${1} lower than ${v}"
1048c2ecf20Sopenharmony_ci}
105