Skip to content

Commit 1044177

Browse files
andrestcNipaLocal
authored andcommitted
selftests: netconsole: validate target resume
Introduce a new netconsole selftest to validate that netconsole is able to resume a deactivated target when the low level interface comes back. The test setups the network using netdevsim, creates a netconsole target and then remove/add netdevsim in order to bring the same interfaces back. Afterwards, the test validates that the target works as expected. Targets are created via cmdline parameters to the module to ensure that we are able to resume targets that were bound by mac and interface name. Signed-off-by: Andre Carvalho <asantostc@gmail.com> Signed-off-by: NipaLocal <nipa@local>
1 parent 5e05173 commit 1044177

File tree

3 files changed

+128
-5
lines changed

3 files changed

+128
-5
lines changed

tools/testing/selftests/drivers/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ TEST_PROGS := \
1717
netcons_cmdline.sh \
1818
netcons_fragmented_msg.sh \
1919
netcons_overflow.sh \
20+
netcons_resume.sh \
2021
netcons_sysdata.sh \
2122
netcons_torture.sh \
2223
netpoll_basic.py \

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,21 @@ function do_cleanup() {
203203
function cleanup_netcons() {
204204
# delete netconsole dynamic reconfiguration
205205
# do not fail if the target is already disabled
206-
if [[ ! -d "${NETCONS_PATH}" ]]
206+
local TARGET_PATH=${1:-${NETCONS_PATH}}
207+
208+
if [[ ! -d "${TARGET_PATH}" ]]
207209
then
208210
# in some cases this is called before netcons path is created
209211
return
210212
fi
211-
if [[ $(cat "${NETCONS_PATH}"/enabled) != 0 ]]
213+
if [[ $(cat "${TARGET_PATH}"/enabled) != 0 ]]
212214
then
213-
echo 0 > "${NETCONS_PATH}"/enabled || true
215+
echo 0 > "${TARGET_PATH}"/enabled || true
214216
fi
215217
# Remove all the keys that got created during the selftest
216-
find "${NETCONS_PATH}/userdata/" -mindepth 1 -type d -delete
218+
find "${TARGET_PATH}/userdata/" -mindepth 1 -type d -delete
217219
# Remove the configfs entry
218-
rmdir "${NETCONS_PATH}"
220+
rmdir "${TARGET_PATH}"
219221
}
220222

221223
function cleanup() {
@@ -377,6 +379,29 @@ function check_netconsole_module() {
377379
fi
378380
}
379381

382+
function wait_target_state() {
383+
local TARGET=${1}
384+
local STATE=${2}
385+
local TARGET_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"
386+
local ENABLED=0
387+
388+
if [ "${STATE}" == "enabled" ]
389+
then
390+
ENABLED=1
391+
fi
392+
393+
if [ ! -d "$TARGET_PATH" ]; then
394+
echo "FAIL: Target does not exist." >&2
395+
exit "${ksft_fail}"
396+
fi
397+
398+
local CHECK_CMD="grep \"$ENABLED\" \"$TARGET_PATH/enabled\""
399+
slowwait 2 sh -c "test -n \"\$($CHECK_CMD)\"" || {
400+
echo "FAIL: ${TARGET} is not ${STATE}." >&2
401+
exit "${ksft_fail}"
402+
}
403+
}
404+
380405
# A wrapper to translate protocol version to udp version
381406
function wait_for_port() {
382407
local NAMESPACE=${1}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
# This test validates that netconsole is able to resume a target that was
5+
# deactivated when its interface was removed when the interface is brought
6+
# back up.
7+
#
8+
# The test configures a netconsole target and then removes netdevsim module to
9+
# cause the interface to disappear. Targets are configured via cmdline to ensure
10+
# targets bound by interface name and mac address can be resumed.
11+
# The test verifies that the target moved to disabled state before adding
12+
# netdevsim and the interface back.
13+
#
14+
# Finally, the test verifies that the target is re-enabled automatically and
15+
# the message is received on the destination interface.
16+
#
17+
# Author: Andre Carvalho <asantostc@gmail.com>
18+
19+
set -euo pipefail
20+
21+
SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
22+
23+
source "${SCRIPTDIR}"/lib/sh/lib_netcons.sh
24+
25+
modprobe netdevsim 2> /dev/null || true
26+
rmmod netconsole 2> /dev/null || true
27+
28+
check_netconsole_module
29+
30+
function cleanup() {
31+
cleanup_netcons "${NETCONS_CONFIGFS}/cmdline0"
32+
do_cleanup
33+
rmmod netconsole
34+
}
35+
36+
trap cleanup EXIT
37+
38+
# Run the test twice, with different cmdline parameters
39+
for BINDMODE in "ifname" "mac"
40+
do
41+
echo "Running with bind mode: ${BINDMODE}" >&2
42+
# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
43+
echo "6 5" > /proc/sys/kernel/printk
44+
45+
# Create one namespace and two interfaces
46+
set_network
47+
48+
# Create the command line for netconsole, with the configuration from
49+
# the function above
50+
CMDLINE=$(create_cmdline_str "${BINDMODE}")
51+
52+
# The content of kmsg will be save to the following file
53+
OUTPUT_FILE="/tmp/${TARGET}-${BINDMODE}"
54+
55+
# Load the module, with the cmdline set
56+
modprobe netconsole "${CMDLINE}"
57+
# Expose cmdline target in configfs
58+
mkdir "${NETCONS_CONFIGFS}/cmdline0"
59+
60+
# Target should be enabled
61+
wait_target_state "cmdline0" "enabled"
62+
63+
# Remove low level module
64+
rmmod netdevsim
65+
# Target should be disabled
66+
wait_target_state "cmdline0" "disabled"
67+
68+
# Add back low level module
69+
modprobe netdevsim
70+
# Recreate namespace and two interfaces
71+
set_network
72+
# Target should be enabled again
73+
wait_target_state "cmdline0" "enabled"
74+
75+
# Listen for netconsole port inside the namespace and destination
76+
# interface
77+
listen_port_and_save_to "${OUTPUT_FILE}" &
78+
# Wait for socat to start and listen to the port.
79+
wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
80+
# Send the message
81+
echo "${MSG}: ${TARGET}" > /dev/kmsg
82+
# Wait until socat saves the file to disk
83+
busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
84+
# Make sure the message was received in the dst part
85+
# and exit
86+
validate_msg "${OUTPUT_FILE}"
87+
88+
# kill socat in case it is still running
89+
pkill_socat
90+
# Cleanup & unload the module
91+
cleanup
92+
93+
echo "${BINDMODE} : Test passed" >&2
94+
done
95+
96+
trap - EXIT
97+
exit "${ksft_pass}"

0 commit comments

Comments
 (0)