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
34*d7a25224SBreno Leitao# Enable the taskname to be appended to sysdata
35*d7a25224SBreno Leitaofunction set_taskname() {
36*d7a25224SBreno Leitao	if [[ ! -f "${NETCONS_PATH}/userdata/taskname_enabled" ]]
37*d7a25224SBreno Leitao	then
38*d7a25224SBreno Leitao		echo "Not able to enable taskname sysdata append. Configfs not available in ${NETCONS_PATH}/userdata/taskname_enabled" >&2
39*d7a25224SBreno Leitao		exit "${ksft_skip}"
40*d7a25224SBreno Leitao	fi
41*d7a25224SBreno Leitao
42*d7a25224SBreno Leitao	echo 1 > "${NETCONS_PATH}/userdata/taskname_enabled"
43*d7a25224SBreno Leitao}
44*d7a25224SBreno Leitao
4512fd83caSBreno Leitao# Disable the sysdata cpu_nr feature
4612fd83caSBreno Leitaofunction unset_cpu_nr() {
4712fd83caSBreno Leitao	echo 0 > "${NETCONS_PATH}/userdata/cpu_nr_enabled"
4812fd83caSBreno Leitao}
4912fd83caSBreno Leitao
50*d7a25224SBreno Leitao# Once called, taskname=<..> will not be appended anymore
51*d7a25224SBreno Leitaofunction unset_taskname() {
52*d7a25224SBreno Leitao	echo 0 > "${NETCONS_PATH}/userdata/taskname_enabled"
53*d7a25224SBreno Leitao}
54*d7a25224SBreno Leitao
55*d7a25224SBreno Leitao# Test if MSG contains sysdata
56*d7a25224SBreno Leitaofunction validate_sysdata() {
5712fd83caSBreno Leitao	# OUTPUT_FILE will contain something like:
5812fd83caSBreno Leitao	# 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM
5912fd83caSBreno Leitao	#  userdatakey=userdatavalue
6012fd83caSBreno Leitao	#  cpu=X
61*d7a25224SBreno Leitao	#  taskname=<taskname>
62*d7a25224SBreno Leitao
63*d7a25224SBreno Leitao	# Echo is what this test uses to create the message. See runtest()
64*d7a25224SBreno Leitao	# function
65*d7a25224SBreno Leitao	SENDER="echo"
6612fd83caSBreno Leitao
6712fd83caSBreno Leitao	if [ ! -f "$OUTPUT_FILE" ]; then
6812fd83caSBreno Leitao		echo "FAIL: File was not generated." >&2
6912fd83caSBreno Leitao		exit "${ksft_fail}"
7012fd83caSBreno Leitao	fi
7112fd83caSBreno Leitao
7212fd83caSBreno Leitao	if ! grep -q "${MSG}" "${OUTPUT_FILE}"; then
7312fd83caSBreno Leitao		echo "FAIL: ${MSG} not found in ${OUTPUT_FILE}" >&2
7412fd83caSBreno Leitao		cat "${OUTPUT_FILE}" >&2
7512fd83caSBreno Leitao		exit "${ksft_fail}"
7612fd83caSBreno Leitao	fi
7712fd83caSBreno Leitao
7812fd83caSBreno Leitao	# Check if cpu=XX exists in the file and matches the one used
7912fd83caSBreno Leitao	# in taskset(1)
8012fd83caSBreno Leitao	if ! grep -q "cpu=${CPU}\+" "${OUTPUT_FILE}"; then
8112fd83caSBreno Leitao		echo "FAIL: 'cpu=${CPU}' not found in ${OUTPUT_FILE}" >&2
8212fd83caSBreno Leitao		cat "${OUTPUT_FILE}" >&2
8312fd83caSBreno Leitao		exit "${ksft_fail}"
8412fd83caSBreno Leitao	fi
8512fd83caSBreno Leitao
86*d7a25224SBreno Leitao	if ! grep -q "taskname=${SENDER}" "${OUTPUT_FILE}"; then
87*d7a25224SBreno Leitao		echo "FAIL: 'taskname=echo' not found in ${OUTPUT_FILE}" >&2
88*d7a25224SBreno Leitao		cat "${OUTPUT_FILE}" >&2
89*d7a25224SBreno Leitao		exit "${ksft_fail}"
90*d7a25224SBreno Leitao	fi
91*d7a25224SBreno Leitao
9212fd83caSBreno Leitao	rm "${OUTPUT_FILE}"
9312fd83caSBreno Leitao	pkill_socat
9412fd83caSBreno Leitao}
9512fd83caSBreno Leitao
96*d7a25224SBreno Leitao# Test if MSG content exists in OUTPUT_FILE but no `cpu=` and `taskname=`
97*d7a25224SBreno Leitao# strings
98*d7a25224SBreno Leitaofunction validate_no_sysdata() {
9912fd83caSBreno Leitao	if [ ! -f "$OUTPUT_FILE" ]; then
10012fd83caSBreno Leitao		echo "FAIL: File was not generated." >&2
10112fd83caSBreno Leitao		exit "${ksft_fail}"
10212fd83caSBreno Leitao	fi
10312fd83caSBreno Leitao
10412fd83caSBreno Leitao	if ! grep -q "${MSG}" "${OUTPUT_FILE}"; then
10512fd83caSBreno Leitao		echo "FAIL: ${MSG} not found in ${OUTPUT_FILE}" >&2
10612fd83caSBreno Leitao		cat "${OUTPUT_FILE}" >&2
10712fd83caSBreno Leitao		exit "${ksft_fail}"
10812fd83caSBreno Leitao	fi
10912fd83caSBreno Leitao
11012fd83caSBreno Leitao	if grep -q "cpu=" "${OUTPUT_FILE}"; then
11112fd83caSBreno Leitao		echo "FAIL: 'cpu=  found in ${OUTPUT_FILE}" >&2
11212fd83caSBreno Leitao		cat "${OUTPUT_FILE}" >&2
11312fd83caSBreno Leitao		exit "${ksft_fail}"
11412fd83caSBreno Leitao	fi
11512fd83caSBreno Leitao
116*d7a25224SBreno Leitao	if grep -q "taskname=" "${OUTPUT_FILE}"; then
117*d7a25224SBreno Leitao		echo "FAIL: 'taskname=  found in ${OUTPUT_FILE}" >&2
118*d7a25224SBreno Leitao		cat "${OUTPUT_FILE}" >&2
119*d7a25224SBreno Leitao		exit "${ksft_fail}"
120*d7a25224SBreno Leitao	fi
121*d7a25224SBreno Leitao
12212fd83caSBreno Leitao	rm "${OUTPUT_FILE}"
12312fd83caSBreno Leitao}
12412fd83caSBreno Leitao
12512fd83caSBreno Leitao# Start socat, send the message and wait for the file to show up in the file
12612fd83caSBreno Leitao# system
12712fd83caSBreno Leitaofunction runtest {
12812fd83caSBreno Leitao	# Listen for netconsole port inside the namespace and destination
12912fd83caSBreno Leitao	# interface
13012fd83caSBreno Leitao	listen_port_and_save_to "${OUTPUT_FILE}" &
13112fd83caSBreno Leitao	# Wait for socat to start and listen to the port.
13212fd83caSBreno Leitao	wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
13312fd83caSBreno Leitao	# Send the message
13412fd83caSBreno Leitao	taskset -c "${CPU}" echo "${MSG}: ${TARGET}" > /dev/kmsg
13512fd83caSBreno Leitao	# Wait until socat saves the file to disk
13612fd83caSBreno Leitao	busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
13712fd83caSBreno Leitao}
13812fd83caSBreno Leitao
13912fd83caSBreno Leitao# ========== #
14012fd83caSBreno Leitao# Start here #
14112fd83caSBreno Leitao# ========== #
14212fd83caSBreno Leitao
14312fd83caSBreno Leitaomodprobe netdevsim 2> /dev/null || true
14412fd83caSBreno Leitaomodprobe netconsole 2> /dev/null || true
14512fd83caSBreno Leitao
14612fd83caSBreno Leitao# Check for basic system dependency and exit if not found
14712fd83caSBreno Leitaocheck_for_dependencies
14812fd83caSBreno Leitao# This test also depends on taskset(1). Check for it before starting the test
14912fd83caSBreno Leitaocheck_for_taskset
15012fd83caSBreno Leitao
15112fd83caSBreno Leitao# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
15212fd83caSBreno Leitaoecho "6 5" > /proc/sys/kernel/printk
15312fd83caSBreno Leitao# Remove the namespace, interfaces and netconsole target on exit
15412fd83caSBreno Leitaotrap cleanup EXIT
15512fd83caSBreno Leitao# Create one namespace and two interfaces
15612fd83caSBreno Leitaoset_network
15712fd83caSBreno Leitao# Create a dynamic target for netconsole
15812fd83caSBreno Leitaocreate_dynamic_target
15912fd83caSBreno Leitao
16012fd83caSBreno Leitao#====================================================
16112fd83caSBreno Leitao# TEST #1
16212fd83caSBreno Leitao# Send message from a random CPU
16312fd83caSBreno Leitao#====================================================
16412fd83caSBreno Leitao# Random CPU in the system
16512fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc)))
16612fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_1"
16712fd83caSBreno LeitaoMSG="Test #1 from CPU${CPU}"
16812fd83caSBreno Leitao# Enable the auto population of cpu_nr
16912fd83caSBreno Leitaoset_cpu_nr
170*d7a25224SBreno Leitao# Enable taskname to be appended to sysdata
171*d7a25224SBreno Leitaoset_taskname
17212fd83caSBreno Leitaoruntest
17312fd83caSBreno Leitao# Make sure the message was received in the dst part
17412fd83caSBreno Leitao# and exit
175*d7a25224SBreno Leitaovalidate_sysdata
17612fd83caSBreno Leitao
17712fd83caSBreno Leitao#====================================================
17812fd83caSBreno Leitao# TEST #2
17912fd83caSBreno Leitao# This test now adds userdata together with sysdata
18012fd83caSBreno Leitao# ===================================================
18112fd83caSBreno Leitao# Get a new random CPU
18212fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc)))
18312fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_2"
18412fd83caSBreno LeitaoMSG="Test #2 from CPU${CPU}"
18512fd83caSBreno Leitaoset_user_data
18612fd83caSBreno Leitaoruntest
187*d7a25224SBreno Leitaovalidate_sysdata
18812fd83caSBreno Leitao
18912fd83caSBreno Leitao# ===================================================
19012fd83caSBreno Leitao# TEST #3
19112fd83caSBreno Leitao# Unset cpu_nr, so, no CPU should be appended.
19212fd83caSBreno Leitao# userdata is still set
19312fd83caSBreno Leitao# ===================================================
19412fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc)))
19512fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_3"
19612fd83caSBreno LeitaoMSG="Test #3 from CPU${CPU}"
19712fd83caSBreno Leitao# Enable the auto population of cpu_nr
19812fd83caSBreno Leitaounset_cpu_nr
199*d7a25224SBreno Leitaounset_taskname
20012fd83caSBreno Leitaoruntest
20112fd83caSBreno Leitao# At this time, cpu= shouldn't be present in the msg
202*d7a25224SBreno Leitaovalidate_no_sysdata
20312fd83caSBreno Leitao
20412fd83caSBreno Leitaoexit "${ksft_pass}"
205