Skip to content

Commit 61f51cc

Browse files
leitaokuba-moo
authored andcommitted
netconsole: selftest: Split the helpers from the selftest
Split helper functions from the netconsole basic test into a separate library file to enable reuse across different netconsole tests. This change only moves the existing helper functions to lib/sh/lib_netcons.sh while preserving the same test functionality. The helpers provide common functions for: - Setting up network namespaces and interfaces - Managing netconsole dynamic targets - Setting user data - Handling test dependencies - Cleanup operations Do not make any change in the code, other than the mechanical separation. Signed-off-by: Breno Leitao <[email protected]> Tested-by: Simon Horman <[email protected]> Reviewed-by: Simon Horman <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent e51c747 commit 61f51cc

File tree

4 files changed

+228
-217
lines changed

4 files changed

+228
-217
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16170,6 +16170,7 @@ M: Breno Leitao <[email protected]>
1617016170
S: Maintained
1617116171
F: Documentation/networking/netconsole.rst
1617216172
F: drivers/net/netconsole.c
16173+
F: tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh
1617316174
F: tools/testing/selftests/drivers/net/netcons_basic.sh
1617416175

1617516176
NETDEVSIM

tools/testing/selftests/drivers/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
TEST_INCLUDES := $(wildcard lib/py/*.py) \
4+
$(wildcard lib/sh/*.sh) \
45
../../net/net_helper.sh \
56
../../net/lib.sh \
67

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

0 commit comments

Comments
 (0)