1*6701896eSBreno Leitao#!/usr/bin/env bash 2*6701896eSBreno Leitao# SPDX-License-Identifier: GPL-2.0 3*6701896eSBreno Leitao 4*6701896eSBreno Leitao# Repeatedly send kernel messages, toggles netconsole targets on and off, 5*6701896eSBreno Leitao# creates and deletes targets in parallel, and toggles the source interface to 6*6701896eSBreno Leitao# simulate stress conditions. 7*6701896eSBreno Leitao# 8*6701896eSBreno Leitao# This test aims to verify the robustness of netconsole under dynamic 9*6701896eSBreno Leitao# configurations and concurrent operations. 10*6701896eSBreno Leitao# 11*6701896eSBreno Leitao# The major goal is to run this test with LOCKDEP, Kmemleak and KASAN to make 12*6701896eSBreno Leitao# sure no issues is reported. 13*6701896eSBreno Leitao# 14*6701896eSBreno Leitao# Author: Breno Leitao <leitao@debian.org> 15*6701896eSBreno Leitao 16*6701896eSBreno Leitaoset -euo pipefail 17*6701896eSBreno Leitao 18*6701896eSBreno LeitaoSCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")") 19*6701896eSBreno Leitao 20*6701896eSBreno Leitaosource "${SCRIPTDIR}"/lib/sh/lib_netcons.sh 21*6701896eSBreno Leitao 22*6701896eSBreno Leitao# Number of times the main loop run 23*6701896eSBreno LeitaoITERATIONS=${1:-150} 24*6701896eSBreno Leitao 25*6701896eSBreno Leitao# Only test extended format 26*6701896eSBreno LeitaoFORMAT="extended" 27*6701896eSBreno Leitao# And ipv6 only 28*6701896eSBreno LeitaoIP_VERSION="ipv6" 29*6701896eSBreno Leitao 30*6701896eSBreno Leitao# Create, enable and delete some targets. 31*6701896eSBreno Leitaocreate_and_delete_random_target() { 32*6701896eSBreno Leitao COUNT=2 33*6701896eSBreno Leitao RND_PREFIX=$(mktemp -u netcons_rnd_XXXX_) 34*6701896eSBreno Leitao 35*6701896eSBreno Leitao if [ -d "${NETCONS_CONFIGFS}/${RND_PREFIX}${COUNT}" ] || \ 36*6701896eSBreno Leitao [ -d "${NETCONS_CONFIGFS}/${RND_PREFIX}0" ]; then 37*6701896eSBreno Leitao echo "Function didn't finish yet, skipping it." >&2 38*6701896eSBreno Leitao return 39*6701896eSBreno Leitao fi 40*6701896eSBreno Leitao 41*6701896eSBreno Leitao # enable COUNT targets 42*6701896eSBreno Leitao for i in $(seq ${COUNT}) 43*6701896eSBreno Leitao do 44*6701896eSBreno Leitao RND_TARGET="${RND_PREFIX}"${i} 45*6701896eSBreno Leitao RND_TARGET_PATH="${NETCONS_CONFIGFS}"/"${RND_TARGET}" 46*6701896eSBreno Leitao 47*6701896eSBreno Leitao # Basic population so the target can come up 48*6701896eSBreno Leitao _create_dynamic_target "${FORMAT}" "${RND_TARGET_PATH}" 49*6701896eSBreno Leitao done 50*6701896eSBreno Leitao 51*6701896eSBreno Leitao echo "netconsole selftest: ${COUNT} additional targets were created" > /dev/kmsg 52*6701896eSBreno Leitao # disable them all 53*6701896eSBreno Leitao for i in $(seq ${COUNT}) 54*6701896eSBreno Leitao do 55*6701896eSBreno Leitao RND_TARGET="${RND_PREFIX}"${i} 56*6701896eSBreno Leitao RND_TARGET_PATH="${NETCONS_CONFIGFS}"/"${RND_TARGET}" 57*6701896eSBreno Leitao if [[ $(cat "${RND_TARGET_PATH}/enabled") -eq 1 ]] 58*6701896eSBreno Leitao then 59*6701896eSBreno Leitao echo 0 > "${RND_TARGET_PATH}"/enabled 60*6701896eSBreno Leitao fi 61*6701896eSBreno Leitao rmdir "${RND_TARGET_PATH}" 62*6701896eSBreno Leitao done 63*6701896eSBreno Leitao} 64*6701896eSBreno Leitao 65*6701896eSBreno Leitao# Disable and enable the target mid-air, while messages 66*6701896eSBreno Leitao# are being transmitted. 67*6701896eSBreno Leitaotoggle_netcons_target() { 68*6701896eSBreno Leitao for i in $(seq 2) 69*6701896eSBreno Leitao do 70*6701896eSBreno Leitao if [ ! -d "${NETCONS_PATH}" ] 71*6701896eSBreno Leitao then 72*6701896eSBreno Leitao break 73*6701896eSBreno Leitao fi 74*6701896eSBreno Leitao echo 0 > "${NETCONS_PATH}"/enabled 2> /dev/null || true 75*6701896eSBreno Leitao # Try to enable a bit harder, given it might fail to enable 76*6701896eSBreno Leitao # Write to `enabled` might fail depending on the lock, which is 77*6701896eSBreno Leitao # highly contentious here 78*6701896eSBreno Leitao for _ in $(seq 5) 79*6701896eSBreno Leitao do 80*6701896eSBreno Leitao echo 1 > "${NETCONS_PATH}"/enabled 2> /dev/null || true 81*6701896eSBreno Leitao done 82*6701896eSBreno Leitao done 83*6701896eSBreno Leitao} 84*6701896eSBreno Leitao 85*6701896eSBreno Leitaotoggle_iface(){ 86*6701896eSBreno Leitao ip link set "${SRCIF}" down 87*6701896eSBreno Leitao ip link set "${SRCIF}" up 88*6701896eSBreno Leitao} 89*6701896eSBreno Leitao 90*6701896eSBreno Leitao# Start here 91*6701896eSBreno Leitao 92*6701896eSBreno Leitaomodprobe netdevsim 2> /dev/null || true 93*6701896eSBreno Leitaomodprobe netconsole 2> /dev/null || true 94*6701896eSBreno Leitao 95*6701896eSBreno Leitao# Check for basic system dependency and exit if not found 96*6701896eSBreno Leitaocheck_for_dependencies 97*6701896eSBreno Leitao# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5) 98*6701896eSBreno Leitaoecho "6 5" > /proc/sys/kernel/printk 99*6701896eSBreno Leitao# Remove the namespace, interfaces and netconsole target on exit 100*6701896eSBreno Leitaotrap cleanup EXIT 101*6701896eSBreno Leitao# Create one namespace and two interfaces 102*6701896eSBreno Leitaoset_network "${IP_VERSION}" 103*6701896eSBreno Leitao# Create a dynamic target for netconsole 104*6701896eSBreno Leitaocreate_dynamic_target "${FORMAT}" 105*6701896eSBreno Leitao 106*6701896eSBreno Leitaofor i in $(seq "$ITERATIONS") 107*6701896eSBreno Leitaodo 108*6701896eSBreno Leitao for _ in $(seq 10) 109*6701896eSBreno Leitao do 110*6701896eSBreno Leitao echo "${MSG}: ${TARGET} ${i}" > /dev/kmsg 111*6701896eSBreno Leitao done 112*6701896eSBreno Leitao wait 113*6701896eSBreno Leitao 114*6701896eSBreno Leitao if (( i % 30 == 0 )); then 115*6701896eSBreno Leitao toggle_netcons_target & 116*6701896eSBreno Leitao fi 117*6701896eSBreno Leitao 118*6701896eSBreno Leitao if (( i % 50 == 0 )); then 119*6701896eSBreno Leitao # create some targets, enable them, send msg and disable 120*6701896eSBreno Leitao # all in a parallel thread 121*6701896eSBreno Leitao create_and_delete_random_target & 122*6701896eSBreno Leitao fi 123*6701896eSBreno Leitao 124*6701896eSBreno Leitao if (( i % 70 == 0 )); then 125*6701896eSBreno Leitao toggle_iface & 126*6701896eSBreno Leitao fi 127*6701896eSBreno Leitaodone 128*6701896eSBreno Leitaowait 129*6701896eSBreno Leitao 130*6701896eSBreno Leitaoexit "${EXIT_STATUS}" 131