1*12fd83caSBreno Leitao#!/usr/bin/env bash 2*12fd83caSBreno Leitao# SPDX-License-Identifier: GPL-2.0 3*12fd83caSBreno Leitao 4*12fd83caSBreno Leitao# A test that makes sure that sysdata runtime CPU data is properly set 5*12fd83caSBreno Leitao# when a message is sent. 6*12fd83caSBreno Leitao# 7*12fd83caSBreno Leitao# There are 3 different tests, every time sent using a random CPU. 8*12fd83caSBreno Leitao# - Test #1 9*12fd83caSBreno Leitao# * Only enable cpu_nr sysdata feature. 10*12fd83caSBreno Leitao# - Test #2 11*12fd83caSBreno Leitao# * Keep cpu_nr sysdata feature enable and enable userdata. 12*12fd83caSBreno Leitao# - Test #3 13*12fd83caSBreno Leitao# * keep userdata enabled, and disable sysdata cpu_nr feature. 14*12fd83caSBreno Leitao# 15*12fd83caSBreno Leitao# Author: Breno Leitao <[email protected]> 16*12fd83caSBreno Leitao 17*12fd83caSBreno Leitaoset -euo pipefail 18*12fd83caSBreno Leitao 19*12fd83caSBreno LeitaoSCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")") 20*12fd83caSBreno Leitao 21*12fd83caSBreno Leitaosource "${SCRIPTDIR}"/lib/sh/lib_netcons.sh 22*12fd83caSBreno Leitao 23*12fd83caSBreno Leitao# Enable the sysdata cpu_nr feature 24*12fd83caSBreno Leitaofunction set_cpu_nr() { 25*12fd83caSBreno Leitao if [[ ! -f "${NETCONS_PATH}/userdata/cpu_nr_enabled" ]] 26*12fd83caSBreno Leitao then 27*12fd83caSBreno Leitao echo "Populate CPU configfs path not available in ${NETCONS_PATH}/userdata/cpu_nr_enabled" >&2 28*12fd83caSBreno Leitao exit "${ksft_skip}" 29*12fd83caSBreno Leitao fi 30*12fd83caSBreno Leitao 31*12fd83caSBreno Leitao echo 1 > "${NETCONS_PATH}/userdata/cpu_nr_enabled" 32*12fd83caSBreno Leitao} 33*12fd83caSBreno Leitao 34*12fd83caSBreno Leitao# Disable the sysdata cpu_nr feature 35*12fd83caSBreno Leitaofunction unset_cpu_nr() { 36*12fd83caSBreno Leitao echo 0 > "${NETCONS_PATH}/userdata/cpu_nr_enabled" 37*12fd83caSBreno Leitao} 38*12fd83caSBreno Leitao 39*12fd83caSBreno Leitao# Test if MSG content and `cpu=${CPU}` exists in OUTPUT_FILE 40*12fd83caSBreno Leitaofunction validate_sysdata_cpu_exists() { 41*12fd83caSBreno Leitao # OUTPUT_FILE will contain something like: 42*12fd83caSBreno Leitao # 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM 43*12fd83caSBreno Leitao # userdatakey=userdatavalue 44*12fd83caSBreno Leitao # cpu=X 45*12fd83caSBreno Leitao 46*12fd83caSBreno Leitao if [ ! -f "$OUTPUT_FILE" ]; then 47*12fd83caSBreno Leitao echo "FAIL: File was not generated." >&2 48*12fd83caSBreno Leitao exit "${ksft_fail}" 49*12fd83caSBreno Leitao fi 50*12fd83caSBreno Leitao 51*12fd83caSBreno Leitao if ! grep -q "${MSG}" "${OUTPUT_FILE}"; then 52*12fd83caSBreno Leitao echo "FAIL: ${MSG} not found in ${OUTPUT_FILE}" >&2 53*12fd83caSBreno Leitao cat "${OUTPUT_FILE}" >&2 54*12fd83caSBreno Leitao exit "${ksft_fail}" 55*12fd83caSBreno Leitao fi 56*12fd83caSBreno Leitao 57*12fd83caSBreno Leitao # Check if cpu=XX exists in the file and matches the one used 58*12fd83caSBreno Leitao # in taskset(1) 59*12fd83caSBreno Leitao if ! grep -q "cpu=${CPU}\+" "${OUTPUT_FILE}"; then 60*12fd83caSBreno Leitao echo "FAIL: 'cpu=${CPU}' not found in ${OUTPUT_FILE}" >&2 61*12fd83caSBreno Leitao cat "${OUTPUT_FILE}" >&2 62*12fd83caSBreno Leitao exit "${ksft_fail}" 63*12fd83caSBreno Leitao fi 64*12fd83caSBreno Leitao 65*12fd83caSBreno Leitao rm "${OUTPUT_FILE}" 66*12fd83caSBreno Leitao pkill_socat 67*12fd83caSBreno Leitao} 68*12fd83caSBreno Leitao 69*12fd83caSBreno Leitao# Test if MSG content exists in OUTPUT_FILE but no `cpu=` string 70*12fd83caSBreno Leitaofunction validate_sysdata_no_cpu() { 71*12fd83caSBreno Leitao if [ ! -f "$OUTPUT_FILE" ]; then 72*12fd83caSBreno Leitao echo "FAIL: File was not generated." >&2 73*12fd83caSBreno Leitao exit "${ksft_fail}" 74*12fd83caSBreno Leitao fi 75*12fd83caSBreno Leitao 76*12fd83caSBreno Leitao if ! grep -q "${MSG}" "${OUTPUT_FILE}"; then 77*12fd83caSBreno Leitao echo "FAIL: ${MSG} not found in ${OUTPUT_FILE}" >&2 78*12fd83caSBreno Leitao cat "${OUTPUT_FILE}" >&2 79*12fd83caSBreno Leitao exit "${ksft_fail}" 80*12fd83caSBreno Leitao fi 81*12fd83caSBreno Leitao 82*12fd83caSBreno Leitao if grep -q "cpu=" "${OUTPUT_FILE}"; then 83*12fd83caSBreno Leitao echo "FAIL: 'cpu= found in ${OUTPUT_FILE}" >&2 84*12fd83caSBreno Leitao cat "${OUTPUT_FILE}" >&2 85*12fd83caSBreno Leitao exit "${ksft_fail}" 86*12fd83caSBreno Leitao fi 87*12fd83caSBreno Leitao 88*12fd83caSBreno Leitao rm "${OUTPUT_FILE}" 89*12fd83caSBreno Leitao} 90*12fd83caSBreno Leitao 91*12fd83caSBreno Leitao# Start socat, send the message and wait for the file to show up in the file 92*12fd83caSBreno Leitao# system 93*12fd83caSBreno Leitaofunction runtest { 94*12fd83caSBreno Leitao # Listen for netconsole port inside the namespace and destination 95*12fd83caSBreno Leitao # interface 96*12fd83caSBreno Leitao listen_port_and_save_to "${OUTPUT_FILE}" & 97*12fd83caSBreno Leitao # Wait for socat to start and listen to the port. 98*12fd83caSBreno Leitao wait_local_port_listen "${NAMESPACE}" "${PORT}" udp 99*12fd83caSBreno Leitao # Send the message 100*12fd83caSBreno Leitao taskset -c "${CPU}" echo "${MSG}: ${TARGET}" > /dev/kmsg 101*12fd83caSBreno Leitao # Wait until socat saves the file to disk 102*12fd83caSBreno Leitao busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}" 103*12fd83caSBreno Leitao} 104*12fd83caSBreno Leitao 105*12fd83caSBreno Leitao# ========== # 106*12fd83caSBreno Leitao# Start here # 107*12fd83caSBreno Leitao# ========== # 108*12fd83caSBreno Leitao 109*12fd83caSBreno Leitaomodprobe netdevsim 2> /dev/null || true 110*12fd83caSBreno Leitaomodprobe netconsole 2> /dev/null || true 111*12fd83caSBreno Leitao 112*12fd83caSBreno Leitao# Check for basic system dependency and exit if not found 113*12fd83caSBreno Leitaocheck_for_dependencies 114*12fd83caSBreno Leitao# This test also depends on taskset(1). Check for it before starting the test 115*12fd83caSBreno Leitaocheck_for_taskset 116*12fd83caSBreno Leitao 117*12fd83caSBreno Leitao# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5) 118*12fd83caSBreno Leitaoecho "6 5" > /proc/sys/kernel/printk 119*12fd83caSBreno Leitao# Remove the namespace, interfaces and netconsole target on exit 120*12fd83caSBreno Leitaotrap cleanup EXIT 121*12fd83caSBreno Leitao# Create one namespace and two interfaces 122*12fd83caSBreno Leitaoset_network 123*12fd83caSBreno Leitao# Create a dynamic target for netconsole 124*12fd83caSBreno Leitaocreate_dynamic_target 125*12fd83caSBreno Leitao 126*12fd83caSBreno Leitao#==================================================== 127*12fd83caSBreno Leitao# TEST #1 128*12fd83caSBreno Leitao# Send message from a random CPU 129*12fd83caSBreno Leitao#==================================================== 130*12fd83caSBreno Leitao# Random CPU in the system 131*12fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc))) 132*12fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_1" 133*12fd83caSBreno LeitaoMSG="Test #1 from CPU${CPU}" 134*12fd83caSBreno Leitao# Enable the auto population of cpu_nr 135*12fd83caSBreno Leitaoset_cpu_nr 136*12fd83caSBreno Leitaoruntest 137*12fd83caSBreno Leitao# Make sure the message was received in the dst part 138*12fd83caSBreno Leitao# and exit 139*12fd83caSBreno Leitaovalidate_sysdata_cpu_exists 140*12fd83caSBreno Leitao 141*12fd83caSBreno Leitao#==================================================== 142*12fd83caSBreno Leitao# TEST #2 143*12fd83caSBreno Leitao# This test now adds userdata together with sysdata 144*12fd83caSBreno Leitao# =================================================== 145*12fd83caSBreno Leitao# Get a new random CPU 146*12fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc))) 147*12fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_2" 148*12fd83caSBreno LeitaoMSG="Test #2 from CPU${CPU}" 149*12fd83caSBreno Leitaoset_user_data 150*12fd83caSBreno Leitaoruntest 151*12fd83caSBreno Leitaovalidate_sysdata_cpu_exists 152*12fd83caSBreno Leitao 153*12fd83caSBreno Leitao# =================================================== 154*12fd83caSBreno Leitao# TEST #3 155*12fd83caSBreno Leitao# Unset cpu_nr, so, no CPU should be appended. 156*12fd83caSBreno Leitao# userdata is still set 157*12fd83caSBreno Leitao# =================================================== 158*12fd83caSBreno LeitaoCPU=$((RANDOM % $(nproc))) 159*12fd83caSBreno LeitaoOUTPUT_FILE="/tmp/${TARGET}_3" 160*12fd83caSBreno LeitaoMSG="Test #3 from CPU${CPU}" 161*12fd83caSBreno Leitao# Enable the auto population of cpu_nr 162*12fd83caSBreno Leitaounset_cpu_nr 163*12fd83caSBreno Leitaoruntest 164*12fd83caSBreno Leitao# At this time, cpu= shouldn't be present in the msg 165*12fd83caSBreno Leitaovalidate_sysdata_no_cpu 166*12fd83caSBreno Leitao 167*12fd83caSBreno Leitaoexit "${ksft_pass}" 168