Skip to content

Commit b494b16

Browse files
leitaokuba-moo
authored andcommitted
net: netconsole: selftests: Create a new netconsole selftest
Adds a selftest that creates two virtual interfaces, assigns one to a new namespace, and assigns IP addresses to both. It listens on the destination interface using socat and configures a dynamic target on netconsole, pointing to the destination IP address. The test then checks if the message was received properly on the destination interface. Signed-off-by: Breno Leitao <[email protected]> Acked-by: Matthieu Baerts (NGI0) <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 9f08ae4 commit b494b16

File tree

4 files changed

+243
-1
lines changed

4 files changed

+243
-1
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15775,6 +15775,7 @@ M: Breno Leitao <[email protected]>
1577515775
S: Maintained
1577615776
F: Documentation/networking/netconsole.rst
1577715777
F: drivers/net/netconsole.c
15778+
F: tools/testing/selftests/drivers/net/netcons_basic.sh
1577815779

1577915780
NETDEVSIM
1578015781
M: Jakub Kicinski <[email protected]>

tools/testing/selftests/drivers/net/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3-
TEST_INCLUDES := $(wildcard lib/py/*.py)
3+
TEST_INCLUDES := $(wildcard lib/py/*.py) \
4+
../../net/net_helper.sh \
5+
../../net/lib.sh \
46

57
TEST_PROGS := \
8+
netcons_basic.sh \
69
ping.py \
710
queues.py \
811
stats.py \
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
CONFIG_IPV6=y
22
CONFIG_NETDEVSIM=m
3+
CONFIG_CONFIGFS_FS=y
4+
CONFIG_NETCONSOLE=m
5+
CONFIG_NETCONSOLE_DYNAMIC=y
6+
CONFIG_NETCONSOLE_EXTENDED_LOG=y
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
# This test creates two netdevsim virtual interfaces, assigns one of them (the
5+
# "destination interface") to a new namespace, and assigns IP addresses to both
6+
# interfaces.
7+
#
8+
# It listens on the destination interface using socat and configures a dynamic
9+
# target on netconsole, pointing to the destination IP address.
10+
#
11+
# Finally, it checks whether the message was received properly on the
12+
# destination interface. Note that this test may pollute the kernel log buffer
13+
# (dmesg) and relies on dynamic configuration and namespaces being configured.
14+
#
15+
# Author: Breno Leitao <[email protected]>
16+
17+
set -euo pipefail
18+
19+
SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
20+
21+
# Simple script to test dynamic targets in netconsole
22+
SRCIF="" # to be populated later
23+
SRCIP=192.168.1.1
24+
DSTIF="" # to be populated later
25+
DSTIP=192.168.1.2
26+
27+
PORT="6666"
28+
MSG="netconsole selftest"
29+
TARGET=$(mktemp -u netcons_XXXXX)
30+
DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/printk)
31+
NETCONS_CONFIGFS="/sys/kernel/config/netconsole"
32+
NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"
33+
# NAMESPACE will be populated by setup_ns with a random value
34+
NAMESPACE=""
35+
36+
# IDs for netdevsim
37+
NSIM_DEV_1_ID=$((256 + RANDOM % 256))
38+
NSIM_DEV_2_ID=$((512 + RANDOM % 256))
39+
40+
# Used to create and delete namespaces
41+
source "${SCRIPTDIR}"/../../net/lib.sh
42+
source "${SCRIPTDIR}"/../../net/net_helper.sh
43+
44+
# Create netdevsim interfaces
45+
create_ifaces() {
46+
local NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device
47+
48+
echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW"
49+
echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW"
50+
udevadm settle 2> /dev/null || true
51+
52+
local NSIM1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_1_ID"
53+
local NSIM2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_2_ID"
54+
55+
# These are global variables
56+
SRCIF=$(find "$NSIM1"/net -maxdepth 1 -type d ! \
57+
-path "$NSIM1"/net -exec basename {} \;)
58+
DSTIF=$(find "$NSIM2"/net -maxdepth 1 -type d ! \
59+
-path "$NSIM2"/net -exec basename {} \;)
60+
}
61+
62+
link_ifaces() {
63+
local NSIM_DEV_SYS_LINK="/sys/bus/netdevsim/link_device"
64+
local SRCIF_IFIDX=$(cat /sys/class/net/"$SRCIF"/ifindex)
65+
local DSTIF_IFIDX=$(cat /sys/class/net/"$DSTIF"/ifindex)
66+
67+
exec {NAMESPACE_FD}</var/run/netns/"${NAMESPACE}"
68+
exec {INITNS_FD}</proc/self/ns/net
69+
70+
# Bind the dst interface to namespace
71+
ip link set "${DSTIF}" netns "${NAMESPACE}"
72+
73+
# Linking one device to the other one (on the other namespace}
74+
if ! echo "${INITNS_FD}:$SRCIF_IFIDX $NAMESPACE_FD:$DSTIF_IFIDX" > $NSIM_DEV_SYS_LINK
75+
then
76+
echo "linking netdevsim1 with netdevsim2 should succeed"
77+
cleanup
78+
exit "${ksft_skip}"
79+
fi
80+
}
81+
82+
function configure_ip() {
83+
# Configure the IPs for both interfaces
84+
ip netns exec "${NAMESPACE}" ip addr add "${DSTIP}"/24 dev "${DSTIF}"
85+
ip netns exec "${NAMESPACE}" ip link set "${DSTIF}" up
86+
87+
ip addr add "${SRCIP}"/24 dev "${SRCIF}"
88+
ip link set "${SRCIF}" up
89+
}
90+
91+
function set_network() {
92+
# setup_ns function is coming from lib.sh
93+
setup_ns NAMESPACE
94+
95+
# Create both interfaces, and assign the destination to a different
96+
# namespace
97+
create_ifaces
98+
99+
# Link both interfaces back to back
100+
link_ifaces
101+
102+
configure_ip
103+
}
104+
105+
function create_dynamic_target() {
106+
DSTMAC=$(ip netns exec "${NAMESPACE}" \
107+
ip link show "${DSTIF}" | awk '/ether/ {print $2}')
108+
109+
# Create a dynamic target
110+
mkdir "${NETCONS_PATH}"
111+
112+
echo "${DSTIP}" > "${NETCONS_PATH}"/remote_ip
113+
echo "${SRCIP}" > "${NETCONS_PATH}"/local_ip
114+
echo "${DSTMAC}" > "${NETCONS_PATH}"/remote_mac
115+
echo "${SRCIF}" > "${NETCONS_PATH}"/dev_name
116+
117+
echo 1 > "${NETCONS_PATH}"/enabled
118+
}
119+
120+
function cleanup() {
121+
local NSIM_DEV_SYS_DEL="/sys/bus/netdevsim/del_device"
122+
123+
# delete netconsole dynamic reconfiguration
124+
echo 0 > "${NETCONS_PATH}"/enabled
125+
# Remove the configfs entry
126+
rmdir "${NETCONS_PATH}"
127+
128+
# Delete netdevsim devices
129+
echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL"
130+
echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL"
131+
132+
# this is coming from lib.sh
133+
cleanup_all_ns
134+
135+
# Restoring printk configurations
136+
echo "${DEFAULT_PRINTK_VALUES}" > /proc/sys/kernel/printk
137+
}
138+
139+
function listen_port_and_save_to() {
140+
local OUTPUT=${1}
141+
# Just wait for 2 seconds
142+
timeout 2 ip netns exec "${NAMESPACE}" \
143+
socat UDP-LISTEN:"${PORT}",fork "${OUTPUT}"
144+
}
145+
146+
function validate_result() {
147+
local TMPFILENAME="$1"
148+
149+
# Check if the file exists
150+
if [ ! -f "$TMPFILENAME" ]; then
151+
echo "FAIL: File was not generated." >&2
152+
exit "${ksft_fail}"
153+
fi
154+
155+
if ! grep -q "${MSG}" "${TMPFILENAME}"; then
156+
echo "FAIL: ${MSG} not found in ${TMPFILENAME}" >&2
157+
cat "${TMPFILENAME}" >&2
158+
exit "${ksft_fail}"
159+
fi
160+
161+
# Delete the file once it is validated, otherwise keep it
162+
# for debugging purposes
163+
rm "${TMPFILENAME}"
164+
exit "${ksft_pass}"
165+
}
166+
167+
function check_for_dependencies() {
168+
if [ "$(id -u)" -ne 0 ]; then
169+
echo "This test must be run as root" >&2
170+
exit "${ksft_skip}"
171+
fi
172+
173+
if ! which socat > /dev/null ; then
174+
echo "SKIP: socat(1) is not available" >&2
175+
exit "${ksft_skip}"
176+
fi
177+
178+
if ! which ip > /dev/null ; then
179+
echo "SKIP: ip(1) is not available" >&2
180+
exit "${ksft_skip}"
181+
fi
182+
183+
if ! which udevadm > /dev/null ; then
184+
echo "SKIP: udevadm(1) is not available" >&2
185+
exit "${ksft_skip}"
186+
fi
187+
188+
if [ ! -d "${NETCONS_CONFIGFS}" ]; then
189+
echo "SKIP: directory ${NETCONS_CONFIGFS} does not exist. Check if NETCONSOLE_DYNAMIC is enabled" >&2
190+
exit "${ksft_skip}"
191+
fi
192+
193+
if ip link show "${DSTIF}" 2> /dev/null; then
194+
echo "SKIP: interface ${DSTIF} exists in the system. Not overwriting it." >&2
195+
exit "${ksft_skip}"
196+
fi
197+
198+
if ip addr list | grep -E "inet.*(${SRCIP}|${DSTIP})" 2> /dev/null; then
199+
echo "SKIP: IPs already in use. Skipping it" >&2
200+
exit "${ksft_skip}"
201+
fi
202+
}
203+
204+
# ========== #
205+
# Start here #
206+
# ========== #
207+
modprobe netdevsim 2> /dev/null || true
208+
modprobe netconsole 2> /dev/null || true
209+
210+
# The content of kmsg will be save to the following file
211+
OUTPUT_FILE="/tmp/${TARGET}"
212+
213+
# Check for basic system dependency and exit if not found
214+
check_for_dependencies
215+
# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
216+
echo "6 5" > /proc/sys/kernel/printk
217+
# Remove the namespace, interfaces and netconsole target on exit
218+
trap cleanup EXIT
219+
# Create one namespace and two interfaces
220+
set_network
221+
# Create a dynamic target for netconsole
222+
create_dynamic_target
223+
# Listed for netconsole port inside the namespace and destination interface
224+
listen_port_and_save_to "${OUTPUT_FILE}" &
225+
# Wait for socat to start and listen to the port.
226+
wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
227+
# Send the message
228+
echo "${MSG}: ${TARGET}" > /dev/kmsg
229+
# Wait until socat saves the file to disk
230+
busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
231+
232+
# Make sure the message was received in the dst part
233+
# and exit
234+
validate_result "${OUTPUT_FILE}"

0 commit comments

Comments
 (0)