Path: blob/master/tools/testing/selftests/drivers/net/netcons_sysdata.sh
26295 views
#!/usr/bin/env bash1# SPDX-License-Identifier: GPL-2.023# A test that makes sure that sysdata runtime CPU data is properly set4# when a message is sent.5#6# There are 3 different tests, every time sent using a random CPU.7# - Test #18# * Only enable cpu_nr sysdata feature.9# - Test #210# * Keep cpu_nr sysdata feature enable and enable userdata.11# - Test #312# * keep userdata enabled, and disable sysdata cpu_nr feature.13#14# Author: Breno Leitao <[email protected]>1516set -euo pipefail1718SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")1920source "${SCRIPTDIR}"/lib/sh/lib_netcons.sh2122# Enable the sysdata cpu_nr feature23function set_cpu_nr() {24if [[ ! -f "${NETCONS_PATH}/userdata/cpu_nr_enabled" ]]25then26echo "Populate CPU configfs path not available in ${NETCONS_PATH}/userdata/cpu_nr_enabled" >&227exit "${ksft_skip}"28fi2930echo 1 > "${NETCONS_PATH}/userdata/cpu_nr_enabled"31}3233# Enable the taskname to be appended to sysdata34function set_taskname() {35if [[ ! -f "${NETCONS_PATH}/userdata/taskname_enabled" ]]36then37echo "Not able to enable taskname sysdata append. Configfs not available in ${NETCONS_PATH}/userdata/taskname_enabled" >&238exit "${ksft_skip}"39fi4041echo 1 > "${NETCONS_PATH}/userdata/taskname_enabled"42}4344# Enable the release to be appended to sysdata45function set_release() {46if [[ ! -f "${NETCONS_PATH}/userdata/release_enabled" ]]47then48echo "Not able to enable release sysdata append. Configfs not available in ${NETCONS_PATH}/userdata/release_enabled" >&249exit "${ksft_skip}"50fi5152echo 1 > "${NETCONS_PATH}/userdata/release_enabled"53}5455# Enable the msgid to be appended to sysdata56function set_msgid() {57if [[ ! -f "${NETCONS_PATH}/userdata/msgid_enabled" ]]58then59echo "Not able to enable msgid sysdata append. Configfs not available in ${NETCONS_PATH}/userdata/msgid_enabled" >&260exit "${ksft_skip}"61fi6263echo 1 > "${NETCONS_PATH}/userdata/msgid_enabled"64}6566# Disable the sysdata cpu_nr feature67function unset_cpu_nr() {68echo 0 > "${NETCONS_PATH}/userdata/cpu_nr_enabled"69}7071# Once called, taskname=<..> will not be appended anymore72function unset_taskname() {73echo 0 > "${NETCONS_PATH}/userdata/taskname_enabled"74}7576function unset_release() {77echo 0 > "${NETCONS_PATH}/userdata/release_enabled"78}7980function unset_msgid() {81echo 0 > "${NETCONS_PATH}/userdata/msgid_enabled"82}8384# Test if MSG contains sysdata85function validate_sysdata() {86# OUTPUT_FILE will contain something like:87# 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM88# userdatakey=userdatavalue89# cpu=X90# taskname=<taskname>91# msgid=<id>9293# Echo is what this test uses to create the message. See runtest()94# function95SENDER="echo"9697if [ ! -f "$OUTPUT_FILE" ]; then98echo "FAIL: File was not generated." >&299exit "${ksft_fail}"100fi101102if ! grep -q "${MSG}" "${OUTPUT_FILE}"; then103echo "FAIL: ${MSG} not found in ${OUTPUT_FILE}" >&2104cat "${OUTPUT_FILE}" >&2105exit "${ksft_fail}"106fi107108# Check if cpu=XX exists in the file and matches the one used109# in taskset(1)110if ! grep -q "cpu=${CPU}\+" "${OUTPUT_FILE}"; then111echo "FAIL: 'cpu=${CPU}' not found in ${OUTPUT_FILE}" >&2112cat "${OUTPUT_FILE}" >&2113exit "${ksft_fail}"114fi115116if ! grep -q "taskname=${SENDER}" "${OUTPUT_FILE}"; then117echo "FAIL: 'taskname=echo' not found in ${OUTPUT_FILE}" >&2118cat "${OUTPUT_FILE}" >&2119exit "${ksft_fail}"120fi121122if ! grep -q "msgid=[0-9]\+$" "${OUTPUT_FILE}"; then123echo "FAIL: 'msgid=<id>' not found in ${OUTPUT_FILE}" >&2124cat "${OUTPUT_FILE}" >&2125exit "${ksft_fail}"126fi127128rm "${OUTPUT_FILE}"129pkill_socat130}131132function validate_release() {133RELEASE=$(uname -r)134135if [ ! -f "$OUTPUT_FILE" ]; then136echo "FAIL: File was not generated." >&2137exit "${ksft_fail}"138fi139140if ! grep -q "release=${RELEASE}" "${OUTPUT_FILE}"; then141echo "FAIL: 'release=${RELEASE}' not found in ${OUTPUT_FILE}" >&2142cat "${OUTPUT_FILE}" >&2143exit "${ksft_fail}"144fi145}146147# Test if MSG content exists in OUTPUT_FILE but no `cpu=` and `taskname=`148# strings149function validate_no_sysdata() {150if [ ! -f "$OUTPUT_FILE" ]; then151echo "FAIL: File was not generated." >&2152exit "${ksft_fail}"153fi154155if ! grep -q "${MSG}" "${OUTPUT_FILE}"; then156echo "FAIL: ${MSG} not found in ${OUTPUT_FILE}" >&2157cat "${OUTPUT_FILE}" >&2158exit "${ksft_fail}"159fi160161if grep -q "cpu=" "${OUTPUT_FILE}"; then162echo "FAIL: 'cpu= found in ${OUTPUT_FILE}" >&2163cat "${OUTPUT_FILE}" >&2164exit "${ksft_fail}"165fi166167if grep -q "taskname=" "${OUTPUT_FILE}"; then168echo "FAIL: 'taskname= found in ${OUTPUT_FILE}" >&2169cat "${OUTPUT_FILE}" >&2170exit "${ksft_fail}"171fi172173if grep -q "release=" "${OUTPUT_FILE}"; then174echo "FAIL: 'release= found in ${OUTPUT_FILE}" >&2175cat "${OUTPUT_FILE}" >&2176exit "${ksft_fail}"177fi178179if grep -q "msgid=" "${OUTPUT_FILE}"; then180echo "FAIL: 'msgid= found in ${OUTPUT_FILE}" >&2181cat "${OUTPUT_FILE}" >&2182exit "${ksft_fail}"183fi184185rm "${OUTPUT_FILE}"186}187188# Start socat, send the message and wait for the file to show up in the file189# system190function runtest {191# Listen for netconsole port inside the namespace and destination192# interface193listen_port_and_save_to "${OUTPUT_FILE}" &194# Wait for socat to start and listen to the port.195wait_local_port_listen "${NAMESPACE}" "${PORT}" udp196# Send the message197taskset -c "${CPU}" echo "${MSG}: ${TARGET}" > /dev/kmsg198# Wait until socat saves the file to disk199busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"200}201202# ========== #203# Start here #204# ========== #205206modprobe netdevsim 2> /dev/null || true207modprobe netconsole 2> /dev/null || true208209# Check for basic system dependency and exit if not found210check_for_dependencies211# This test also depends on taskset(1). Check for it before starting the test212check_for_taskset213214# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)215echo "6 5" > /proc/sys/kernel/printk216# Remove the namespace, interfaces and netconsole target on exit217trap cleanup EXIT218# Create one namespace and two interfaces219set_network220# Create a dynamic target for netconsole221create_dynamic_target222223#====================================================224# TEST #1225# Send message from a random CPU226#====================================================227# Random CPU in the system228CPU=$((RANDOM % $(nproc)))229OUTPUT_FILE="/tmp/${TARGET}_1"230MSG="Test #1 from CPU${CPU}"231# Enable the auto population of cpu_nr232set_cpu_nr233# Enable taskname to be appended to sysdata234set_taskname235set_release236set_msgid237runtest238# Make sure the message was received in the dst part239# and exit240validate_release241validate_sysdata242243#====================================================244# TEST #2245# This test now adds userdata together with sysdata246# ===================================================247# Get a new random CPU248CPU=$((RANDOM % $(nproc)))249OUTPUT_FILE="/tmp/${TARGET}_2"250MSG="Test #2 from CPU${CPU}"251set_user_data252runtest253validate_release254validate_sysdata255256# ===================================================257# TEST #3258# Unset all sysdata, fail if any userdata is set259# ===================================================260CPU=$((RANDOM % $(nproc)))261OUTPUT_FILE="/tmp/${TARGET}_3"262MSG="Test #3 from CPU${CPU}"263unset_cpu_nr264unset_taskname265unset_release266unset_msgid267runtest268# At this time, cpu= shouldn't be present in the msg269validate_no_sysdata270271exit "${ksft_pass}"272273274