Skip to content

Commit 69d094e

Browse files
leitaokuba-moo
authored andcommitted
selftests: net: add netconsole test for cmdline configuration
Add a new selftest to verify netconsole module loading with command line arguments. This test exercises the init_netconsole() path and validates proper parsing of the netconsole= parameter format. The test: - Loads netconsole module with cmdline configuration instead of dynamic reconfiguration - Validates message transmission through the configured target - Adds helper functions for cmdline string generation and module validation This complements existing netconsole selftests by covering the module initialization code path that processes boot-time parameters. This test is useful to test issues like the one described in [1]. Link: https://lore.kernel.org/netdev/[email protected]/ [1] Signed-off-by: Breno Leitao <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent bed365c commit 69d094e

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

tools/testing/selftests/drivers/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TEST_GEN_FILES := \
1212
TEST_PROGS := \
1313
napi_id.py \
1414
netcons_basic.sh \
15+
netcons_cmdline.sh \
1516
netcons_fragmented_msg.sh \
1617
netcons_overflow.sh \
1718
netcons_sysdata.sh \

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ function create_dynamic_target() {
121121
echo 1 > "${NETCONS_PATH}"/enabled
122122
}
123123

124+
# Generate the command line argument for netconsole following:
125+
# netconsole=[+][src-port]@[src-ip]/[<dev>],[tgt-port]@<tgt-ip>/[tgt-macaddr]
126+
function create_cmdline_str() {
127+
DSTMAC=$(ip netns exec "${NAMESPACE}" \
128+
ip link show "${DSTIF}" | awk '/ether/ {print $2}')
129+
SRCPORT="1514"
130+
TGTPORT="6666"
131+
132+
echo "netconsole=\"+${SRCPORT}@${SRCIP}/${SRCIF},${TGTPORT}@${DSTIP}/${DSTMAC}\""
133+
}
134+
124135
# Do not append the release to the header of the message
125136
function disable_release_append() {
126137
echo 0 > "${NETCONS_PATH}"/enabled
@@ -173,13 +184,9 @@ function listen_port_and_save_to() {
173184
socat UDP-LISTEN:"${PORT}",fork "${OUTPUT}"
174185
}
175186

176-
function validate_result() {
187+
# Only validate that the message arrived properly
188+
function validate_msg() {
177189
local TMPFILENAME="$1"
178-
local FORMAT=${2:-"extended"}
179-
180-
# TMPFILENAME will contain something like:
181-
# 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM
182-
# key=value
183190

184191
# Check if the file exists
185192
if [ ! -f "$TMPFILENAME" ]; then
@@ -192,6 +199,17 @@ function validate_result() {
192199
cat "${TMPFILENAME}" >&2
193200
exit "${ksft_fail}"
194201
fi
202+
}
203+
204+
# Validate the message and userdata
205+
function validate_result() {
206+
local TMPFILENAME="$1"
207+
208+
# TMPFILENAME will contain something like:
209+
# 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM
210+
# key=value
211+
212+
validate_msg "${TMPFILENAME}"
195213

196214
# userdata is not supported on basic format target,
197215
# thus, do not validate it.
@@ -267,3 +285,12 @@ function pkill_socat() {
267285
pkill -f "${PROCESS_NAME}"
268286
set -e
269287
}
288+
289+
# Check if netconsole was compiled as a module, otherwise exit
290+
function check_netconsole_module() {
291+
if modinfo netconsole | grep filename: | grep -q builtin
292+
then
293+
echo "SKIP: netconsole should be compiled as a module" >&2
294+
exit "${ksft_skip}"
295+
fi
296+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
# This is a selftest to test cmdline arguments on netconsole.
5+
# It exercises loading of netconsole from cmdline instead of the dynamic
6+
# reconfiguration. This includes parsing the long netconsole= line and all the
7+
# flow through init_netconsole().
8+
#
9+
# Author: Breno Leitao <[email protected]>
10+
11+
set -euo pipefail
12+
13+
SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
14+
15+
source "${SCRIPTDIR}"/lib/sh/lib_netcons.sh
16+
17+
check_netconsole_module
18+
19+
modprobe netdevsim 2> /dev/null || true
20+
rmmod netconsole 2> /dev/null || true
21+
22+
# The content of kmsg will be save to the following file
23+
OUTPUT_FILE="/tmp/${TARGET}"
24+
25+
# Check for basic system dependency and exit if not found
26+
# check_for_dependencies
27+
# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
28+
echo "6 5" > /proc/sys/kernel/printk
29+
# Remove the namespace and network interfaces
30+
trap do_cleanup EXIT
31+
# Create one namespace and two interfaces
32+
set_network
33+
# Create the command line for netconsole, with the configuration from the
34+
# function above
35+
CMDLINE="$(create_cmdline_str)"
36+
37+
# Load the module, with the cmdline set
38+
modprobe netconsole "${CMDLINE}"
39+
40+
# Listed for netconsole port inside the namespace and destination interface
41+
listen_port_and_save_to "${OUTPUT_FILE}" &
42+
# Wait for socat to start and listen to the port.
43+
wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
44+
# Send the message
45+
echo "${MSG}: ${TARGET}" > /dev/kmsg
46+
# Wait until socat saves the file to disk
47+
busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
48+
# Make sure the message was received in the dst part
49+
# and exit
50+
validate_msg "${OUTPUT_FILE}"
51+
52+
exit "${ksft_pass}"

0 commit comments

Comments
 (0)