112fd83caSBreno Leitao#!/usr/bin/env bash
212fd83caSBreno Leitao# SPDX-License-Identifier: GPL-2.0
312fd83caSBreno Leitao
412fd83caSBreno Leitao# A test that makes sure that sysdata runtime CPU data is properly set
512fd83caSBreno Leitao# when a message is sent.
612fd83caSBreno Leitao#
712fd83caSBreno Leitao# There are 3 different tests, every time sent using a random CPU.
812fd83caSBreno Leitao#  - Test #1
912fd83caSBreno Leitao#    * Only enable cpu_nr sysdata feature.
1012fd83caSBreno Leitao#  - Test #2
1112fd83caSBreno Leitao#    * Keep cpu_nr sysdata feature enable and enable userdata.
1212fd83caSBreno Leitao#  - Test #3
1312fd83caSBreno Leitao#    * keep userdata enabled, and disable sysdata cpu_nr feature.
1412fd83caSBreno Leitao#
1512fd83caSBreno Leitao# Author: Breno Leitao <[email protected]>
1612fd83caSBreno Leitao
1712fd83caSBreno Leitaoset -euo pipefail
1812fd83caSBreno Leitao
1912fd83caSBreno LeitaoSCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
2012fd83caSBreno Leitao
2112fd83caSBreno Leitaosource "${SCRIPTDIR}"/lib/sh/lib_netcons.sh
2212fd83caSBreno Leitao
2312fd83caSBreno Leitao# Enable the sysdata cpu_nr feature
2412fd83caSBreno Leitaofunction set_cpu_nr() {
2512fd83caSBreno Leitao	if [[ ! -f "${NETCONS_PATH}/userdata/cpu_nr_enabled" ]]
2612fd83caSBreno Leitao	then
2712fd83caSBreno Leitao		echo "Populate CPU configfs path not available in ${NETCONS_PATH}/userdata/cpu_nr_enabled" >&2
2812fd83caSBreno Leitao		exit "${ksft_skip}"
2912fd83caSBreno Leitao	fi
3012fd83caSBreno Leitao
3112fd83caSBreno Leitao	echo 1 > "${NETCONS_PATH}/userdata/cpu_nr_enabled"
3212fd83caSBreno Leitao}
3312fd83caSBreno Leitao
34d7a25224SBreno Leitao# Enable the taskname to be appended to sysdata
35d7a25224SBreno Leitaofunction set_taskname() {
36d7a25224SBreno Leitao	if [[ ! -f "${NETCONS_PATH}/userdata/taskname_enabled" ]]
37d7a25224SBreno Leitao	then
38d7a25224SBreno Leitao		echo "Not able to enable taskname sysdata append. Configfs not available in ${NETCONS_PATH}/userdata/taskname_enabled" >&2
39d7a25224SBreno Leitao		exit "${ksft_skip}"
40d7a25224SBreno Leitao	fi
41d7a25224SBreno Leitao
42d7a25224SBreno Leitao	echo 1 > "${NETCONS_PATH}/userdata/taskname_enabled"
43d7a25224SBreno Leitao}
44d7a25224SBreno Leitao
45*4b73dc83SBreno Leitao# Enable the release to be appended to sysdata
46*4b73dc83SBreno Leitaofunction set_release() {
47*4b73dc83SBreno Leitao	if [[ ! -f "${NETCONS_PATH}/userdata/release_enabled" ]]
48*4b73dc83SBreno Leitao	then
49*4b73dc83SBreno Leitao		echo "Not able to enable release sysdata append. Configfs not available in ${NETCONS_PATH}/userdata/release_enabled" >&2
50*4b73dc83SBreno Leitao		exit "${ksft_skip}"
51*4b73dc83SBreno Leitao	fi
52*4b73dc83SBreno Leitao
53*4b73dc83SBreno Leitao	echo 1 > "${NETCONS_PATH}/userdata/release_enabled"
54*4b73dc83SBreno Leitao}
55*4b73dc83SBreno Leitao
5612fd83caSBreno Leitao# Disable the sysdata cpu_nr feature
5712fd83caSBreno Leitaofunction unset_cpu_nr() {
5812fd83caSBreno Leitao	echo 0 > "${NETCONS_PATH}/userdata/cpu_nr_enabled"
5912fd83caSBreno Leitao}
6012fd83caSBreno Leitao
61d7a25224SBreno Leitao# Once called, taskname=<..> will not be appended anymore
62d7a25224SBreno Leitaofunction unset_taskname() {
63d7a25224SBreno Leitao	echo 0 > "${NETCONS_PATH}/userdata/taskname_enabled"
64d7a25224SBreno Leitao}
65d7a25224SBreno Leitao
66*4b73dc83SBreno Leitaofunction unset_release() {
67*4b73dc83SBreno Leitao	echo 0 > "${NETCONS_PATH}/userdata/release_enabled"
68*4b73dc83SBreno Leitao}
69*4b73dc83SBreno Leitao
70d7a25224SBreno Leitao# Test if MSG contains sysdata
71d7a25224SBreno Leitaofunction validate_sysdata() {
7212fd83caSBreno Leitao	# OUTPUT_FILE will contain something like:
7312fd83caSBreno Leitao	# 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM
7412fd83caSBreno Leitao	#  userdatakey=userdatavalue
7512fd83caSBreno Leitao	#  cpu=X
76d7a25224SBreno Leitao	#  taskname=<taskname>
77d7a25224SBreno Leitao
78d7a25224SBreno Leitao	# Echo is what this test uses to create the message. See runtest()
79d7a25224SBreno Leitao	# function
80d7a25224SBreno Leitao	SENDER="echo"
8112fd83caSBreno Leitao
8212fd83caSBreno Leitao	if [ ! -f "$OUTPUT_FILE" ]; then
8312fd83caSBreno Leitao		echo "FAIL: File was not generated." >&2
8412fd83caSBreno Leitao		exit "${ksft_fail}"
8512fd83caSBreno Leitao	fi
8612fd83caSBreno Leitao
8712fd83caSBreno Leitao	if ! grep -q "${MSG}" "${OUTPUT_FILE}"; then
8812fd83caSBreno Leitao		echo "FAIL: ${MSG} not found in ${OUTPUT_FILE}" >&2
8912fd83caSBreno Leitao		cat "${OUTPUT_FILE}" >&2
9012fd83caSBreno Leitao		exit "${ksft_fail}"
9112fd83caSBreno Leitao	fi
9212fd83caSBreno Leitao
9312fd83caSBreno Leitao	# Check if cpu=XX exists in the file and matches the one used
9412fd83caSBreno Leitao	# in taskset(1)
9512fd83caSBreno Leitao	if ! grep -q "cpu=${CPU}\+" "${OUTPUT_FILE}"; then
9612fd83caSBreno Leitao		echo "FAIL: 'cpu=${CPU}' not found in ${OUTPUT_FILE}" >&2
9712fd83caSBreno Leitao		cat "${OUTPUT_FILE}" >&2
9812fd83caSBreno Leitao		exit "${ksft_fail}"
9912fd83caSBreno Leitao	fi
10012fd83caSBreno Leitao
101d7a25224SBreno Leitao	if ! grep -q "taskname=${SENDER}" "${OUTPUT_FILE}"; then
102d7a25224SBreno Leitao		echo "FAIL: 'taskname=echo' not found in ${OUTPUT_FILE}" >&2
103d7a25224SBreno Leitao		cat "${OUTPUT_FILE}" >&2
104d7a25224SBreno Leitao		exit "${ksft_fail}"
105d7a25224SBreno Leitao	fi
106d7a25224SBreno Leitao
10712fd83caSBreno Leitao	rm "${OUTPUT_FILE}"
10812fd83caSBreno Leitao	pkill_socat
10912fd83caSBreno Leitao}
11012fd83caSBreno Leitao
111*4b73dc83SBreno Leitaofunction validate_release() {
112*4b73dc83SBreno Leitao	RELEASE=$(uname -r)
113*4b73dc83SBreno Leitao
114*4b73dc83SBreno Leitao	if [ ! -f "$OUTPUT_FILE" ]; then
115*4b73dc83SBreno Leitao		echo "FAIL: File was not generated." >&2
116*4b73dc83SBreno Leitao		exit "${ksft_fail}"
117*4b73dc83SBreno Leitao	fi
118*4b73dc83SBreno Leitao
119*4b73dc83SBreno Leitao	if ! grep -q "release=${RELEASE}" "${OUTPUT_FILE}"; then
120*4b73dc83SBreno Leitao		echo "FAIL: 'release=${RELEASE}' not found in ${OUTPUT_FILE}" >&2
121*4b73dc83SBreno Leitao		cat "${OUTPUT_FILE}" >&2
122*4b73dc83SBreno Leitao		exit "${ksft_fail}"
123*4b73dc83SBreno Leitao	fi
124*4b73dc83SBreno Leitao}
125*4b73dc83SBreno Leitao
126d7a25224SBreno Leitao# Test if MSG content exists in OUTPUT_FILE but no `cpu=` and `taskname=`
127d7a25224SBreno Leitao# strings
128d7a25224SBreno Leitaofunction validate_no_sysdata() {
12912fd83caSBreno Leitao	if [ ! -f "$OUTPUT_FILE" ]; then
13012fd83caSBreno Leitao		echo "FAIL: File was not generated." >&2
13112fd83caSBreno Leitao		exit "${ksft_fail}"
13212fd83caSBreno Leitao	fi
13312fd83caSBreno Leitao
13412fd83caSBreno Leitao	if ! grep -q "${MSG}" "${OUTPUT_FILE}"; then
13512fd83caSBreno Leitao		echo "FAIL: ${MSG} not found in ${OUTPUT_FILE}" >&2
13612fd83caSBreno Leitao		cat "${OUTPUT_FILE}" >&2
13712fd83caSBreno Leitao		exit "${ksft_fail}"
13812fd83caSBreno Leitao	fi
13912fd83caSBreno Leitao
14012fd83caSBreno Leitao	if grep -q "cpu=" "${OUTPUT_FILE}"; then
14112fd83caSBreno Leitao		echo "FAIL: 'cpu=  found in ${OUTPUT_FILE}" >&2
14212fd83caSBreno Leitao		cat "${OUTPUT_FILE}" >&2
14312fd83caSBreno Leitao		exit "${ksft_fail}"
14412fd83caSBreno Leitao	fi
14512fd83caSBreno Leitao
146d7a25224SBreno Leitao	if grep -q "taskname=" "${OUTPUT_FILE}"; then
147d7a25224SBreno Leitao		echo "FAIL: 'taskname=  found in ${OUTPUT_FILE}" >&2
148d7a25224SBreno Leitao		cat "${OUTPUT_FILE}" >&2
149d7a25224SBreno Leitao		exit "${ksft_fail}"
150d7a25224SBreno Leitao	fi
151d7a25224SBreno Leitao
152*4b73dc83SBreno Leitao	if grep -q "release=" "${OUTPUT_FILE}"; then
153*4b73dc83SBreno Leitao		echo "FAIL: 'release=  found in ${OUTPUT_FILE}" >&2
154*4b73dc83SBreno Leitao		cat "${OUTPUT_FILE}" >&2
155*4b73dc83SBreno Leitao		exit "${ksft_fail}"
156*4b73dc83SBreno Leitao	fi
157*4b73dc83SBreno Leitao
15812fd83caSBreno Leitao	rm "${OUTPUT_FILE}"
15912fd83caSBreno Leitao}
16012fd83caSBreno Leitao
16112fd83caSBreno Leitao# Start socat, send the message and wait for the file to show up in the file
16212fd83caSBreno Leitao# system
16312fd83caSBreno Leitaofunction runtest {
16412fd83caSBreno Leitao	# Listen for netconsole port inside the namespace and destination
16512fd83caSBreno Leitao	# interface
16612fd83caSBreno Leitao	listen_port_and_save_to "${OUTPUT_FILE}" &
16712fd83caSBreno Leitao	# Wait for socat to start and listen to the port.
16812fd83caSBreno Leitao	wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
16912fd83caSBreno Leitao	# Send the message
17012fd83caSBreno Leitao	taskset -c "${CPU}" echo "${MSG}: ${TARGET}" > /dev/kmsg
17112fd83caSBreno Leitao	# Wait until socat saves the file to disk
17212fd83caSBreno Leitao	busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
17312fd83caSBreno Leitao}
17412fd83caSBreno Leitao
17512fd83caSBreno Leitao# ========== #
17612fd83caSBreno Leitao# Start here #
17712fd83caSBreno Leitao# ========== #
17812fd83caSBreno Leitao
17912fd83caSBreno Leitaomodprobe netdevsim 2> /dev/null || true
18012fd83caSBreno Leitaomodprobe netconsole 2> /dev/null || true
18112fd83caSBreno Leitao
18212fd83caSBreno Leitao# Check for basic system dependency and exit if not found
18312fd83caSBreno Leitaocheck_for_dependencies
18412fd83caSBreno Leitao# This test also depends on taskset(1). Check for it before starting the test
18512fd83caSBreno Leitaocheck_for_taskset
18612fd83caSBreno Leitao
18712fd83caSBreno Leitao# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
18812fd83caSBreno Leitaoecho "6 5" > /proc/sys/kernel/printk
18912fd83caSBreno Leitao# Remove the namespace, interfaces and netconsole target on exit
19012fd83caSBreno Leitaotrap cleanup EXIT
19112fd83caSBreno Leitao# Create one namespace and two interfaces
19212fd83caSBreno Leitaoset_network
19312fd83caSBreno Leitao# Create a dynamic target for netconsole
19412fd83caSBreno Leitaocreate_dynamic_target
19512fd83caSBreno Leitao
19612fd83caSBreno Leitao#====================================================
19712fd83caSBreno Leitao# TEST #1
19812fd83caSBreno Leitao# Send message from a random CPU
19912fd83caSBreno Leitao#====================================================
20012fd83caSBreno Leitao# Random CPU in the system
20112fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc)))
20212fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_1"
20312fd83caSBreno LeitaoMSG="Test #1 from CPU${CPU}"
20412fd83caSBreno Leitao# Enable the auto population of cpu_nr
20512fd83caSBreno Leitaoset_cpu_nr
206d7a25224SBreno Leitao# Enable taskname to be appended to sysdata
207d7a25224SBreno Leitaoset_taskname
208*4b73dc83SBreno Leitaoset_release
20912fd83caSBreno Leitaoruntest
21012fd83caSBreno Leitao# Make sure the message was received in the dst part
21112fd83caSBreno Leitao# and exit
212*4b73dc83SBreno Leitaovalidate_release
213d7a25224SBreno Leitaovalidate_sysdata
21412fd83caSBreno Leitao
21512fd83caSBreno Leitao#====================================================
21612fd83caSBreno Leitao# TEST #2
21712fd83caSBreno Leitao# This test now adds userdata together with sysdata
21812fd83caSBreno Leitao# ===================================================
21912fd83caSBreno Leitao# Get a new random CPU
22012fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc)))
22112fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_2"
22212fd83caSBreno LeitaoMSG="Test #2 from CPU${CPU}"
22312fd83caSBreno Leitaoset_user_data
22412fd83caSBreno Leitaoruntest
225*4b73dc83SBreno Leitaovalidate_release
226d7a25224SBreno Leitaovalidate_sysdata
22712fd83caSBreno Leitao
22812fd83caSBreno Leitao# ===================================================
22912fd83caSBreno Leitao# TEST #3
230*4b73dc83SBreno Leitao# Unset all sysdata, fail if any userdata is set
23112fd83caSBreno Leitao# ===================================================
23212fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc)))
23312fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_3"
23412fd83caSBreno LeitaoMSG="Test #3 from CPU${CPU}"
23512fd83caSBreno Leitaounset_cpu_nr
236d7a25224SBreno Leitaounset_taskname
237*4b73dc83SBreno Leitaounset_release
23812fd83caSBreno Leitaoruntest
23912fd83caSBreno Leitao# At this time, cpu= shouldn't be present in the msg
240d7a25224SBreno Leitaovalidate_no_sysdata
24112fd83caSBreno Leitao
24212fd83caSBreno Leitaoexit "${ksft_pass}"
243