Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 <leitao@debian.org>
16
17set -euo pipefail
18
19SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
20
21# Simple script to test dynamic targets in netconsole
22SRCIF="" # to be populated later
23SRCIP=192.0.2.1
24DSTIF="" # to be populated later
25DSTIP=192.0.2.2
26
27PORT="6666"
28MSG="netconsole selftest"
29USERDATA_KEY="key"
30USERDATA_VALUE="value"
31TARGET=$(mktemp -u netcons_XXXXX)
32DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/printk)
33NETCONS_CONFIGFS="/sys/kernel/config/netconsole"
34NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"
35KEY_PATH="${NETCONS_PATH}/userdata/${USERDATA_KEY}"
36# NAMESPACE will be populated by setup_ns with a random value
37NAMESPACE=""
38
39# IDs for netdevsim
40NSIM_DEV_1_ID=$((256 + RANDOM % 256))
41NSIM_DEV_2_ID=$((512 + RANDOM % 256))
42NSIM_DEV_SYS_NEW="/sys/bus/netdevsim/new_device"
43
44# Used to create and delete namespaces
45source "${SCRIPTDIR}"/../../net/lib.sh
46source "${SCRIPTDIR}"/../../net/net_helper.sh
47
48# Create netdevsim interfaces
49create_ifaces() {
50
51 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW"
52 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW"
53 udevadm settle 2> /dev/null || true
54
55 local NSIM1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_1_ID"
56 local NSIM2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_2_ID"
57
58 # These are global variables
59 SRCIF=$(find "$NSIM1"/net -maxdepth 1 -type d ! \
60 -path "$NSIM1"/net -exec basename {} \;)
61 DSTIF=$(find "$NSIM2"/net -maxdepth 1 -type d ! \
62 -path "$NSIM2"/net -exec basename {} \;)
63}
64
65link_ifaces() {
66 local NSIM_DEV_SYS_LINK="/sys/bus/netdevsim/link_device"
67 local SRCIF_IFIDX=$(cat /sys/class/net/"$SRCIF"/ifindex)
68 local DSTIF_IFIDX=$(cat /sys/class/net/"$DSTIF"/ifindex)
69
70 exec {NAMESPACE_FD}</var/run/netns/"${NAMESPACE}"
71 exec {INITNS_FD}</proc/self/ns/net
72
73 # Bind the dst interface to namespace
74 ip link set "${DSTIF}" netns "${NAMESPACE}"
75
76 # Linking one device to the other one (on the other namespace}
77 if ! echo "${INITNS_FD}:$SRCIF_IFIDX $NAMESPACE_FD:$DSTIF_IFIDX" > $NSIM_DEV_SYS_LINK
78 then
79 echo "linking netdevsim1 with netdevsim2 should succeed"
80 cleanup
81 exit "${ksft_skip}"
82 fi
83}
84
85function configure_ip() {
86 # Configure the IPs for both interfaces
87 ip netns exec "${NAMESPACE}" ip addr add "${DSTIP}"/24 dev "${DSTIF}"
88 ip netns exec "${NAMESPACE}" ip link set "${DSTIF}" up
89
90 ip addr add "${SRCIP}"/24 dev "${SRCIF}"
91 ip link set "${SRCIF}" up
92}
93
94function set_network() {
95 # setup_ns function is coming from lib.sh
96 setup_ns NAMESPACE
97
98 # Create both interfaces, and assign the destination to a different
99 # namespace
100 create_ifaces
101
102 # Link both interfaces back to back
103 link_ifaces
104
105 configure_ip
106}
107
108function create_dynamic_target() {
109 DSTMAC=$(ip netns exec "${NAMESPACE}" \
110 ip link show "${DSTIF}" | awk '/ether/ {print $2}')
111
112 # Create a dynamic target
113 mkdir "${NETCONS_PATH}"
114
115 echo "${DSTIP}" > "${NETCONS_PATH}"/remote_ip
116 echo "${SRCIP}" > "${NETCONS_PATH}"/local_ip
117 echo "${DSTMAC}" > "${NETCONS_PATH}"/remote_mac
118 echo "${SRCIF}" > "${NETCONS_PATH}"/dev_name
119
120 echo 1 > "${NETCONS_PATH}"/enabled
121}
122
123function cleanup() {
124 local NSIM_DEV_SYS_DEL="/sys/bus/netdevsim/del_device"
125
126 # delete netconsole dynamic reconfiguration
127 echo 0 > "${NETCONS_PATH}"/enabled
128 # Remove key
129 rmdir "${KEY_PATH}"
130 # Remove the configfs entry
131 rmdir "${NETCONS_PATH}"
132
133 # Delete netdevsim devices
134 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL"
135 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL"
136
137 # this is coming from lib.sh
138 cleanup_all_ns
139
140 # Restoring printk configurations
141 echo "${DEFAULT_PRINTK_VALUES}" > /proc/sys/kernel/printk
142}
143
144function set_user_data() {
145 if [[ ! -d "${NETCONS_PATH}""/userdata" ]]
146 then
147 echo "Userdata path not available in ${NETCONS_PATH}/userdata"
148 exit "${ksft_skip}"
149 fi
150
151 mkdir -p "${KEY_PATH}"
152 VALUE_PATH="${KEY_PATH}""/value"
153 echo "${USERDATA_VALUE}" > "${VALUE_PATH}"
154}
155
156function listen_port_and_save_to() {
157 local OUTPUT=${1}
158 # Just wait for 2 seconds
159 timeout 2 ip netns exec "${NAMESPACE}" \
160 socat UDP-LISTEN:"${PORT}",fork "${OUTPUT}"
161}
162
163function validate_result() {
164 local TMPFILENAME="$1"
165
166 # TMPFILENAME will contain something like:
167 # 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM
168 # key=value
169
170 # Check if the file exists
171 if [ ! -f "$TMPFILENAME" ]; then
172 echo "FAIL: File was not generated." >&2
173 exit "${ksft_fail}"
174 fi
175
176 if ! grep -q "${MSG}" "${TMPFILENAME}"; then
177 echo "FAIL: ${MSG} not found in ${TMPFILENAME}" >&2
178 cat "${TMPFILENAME}" >&2
179 exit "${ksft_fail}"
180 fi
181
182 if ! grep -q "${USERDATA_KEY}=${USERDATA_VALUE}" "${TMPFILENAME}"; then
183 echo "FAIL: ${USERDATA_KEY}=${USERDATA_VALUE} not found in ${TMPFILENAME}" >&2
184 cat "${TMPFILENAME}" >&2
185 exit "${ksft_fail}"
186 fi
187
188 # Delete the file once it is validated, otherwise keep it
189 # for debugging purposes
190 rm "${TMPFILENAME}"
191 exit "${ksft_pass}"
192}
193
194function check_for_dependencies() {
195 if [ "$(id -u)" -ne 0 ]; then
196 echo "This test must be run as root" >&2
197 exit "${ksft_skip}"
198 fi
199
200 if ! which socat > /dev/null ; then
201 echo "SKIP: socat(1) is not available" >&2
202 exit "${ksft_skip}"
203 fi
204
205 if ! which ip > /dev/null ; then
206 echo "SKIP: ip(1) is not available" >&2
207 exit "${ksft_skip}"
208 fi
209
210 if ! which udevadm > /dev/null ; then
211 echo "SKIP: udevadm(1) is not available" >&2
212 exit "${ksft_skip}"
213 fi
214
215 if [ ! -f "${NSIM_DEV_SYS_NEW}" ]; then
216 echo "SKIP: file ${NSIM_DEV_SYS_NEW} does not exist. Check if CONFIG_NETDEVSIM is enabled" >&2
217 exit "${ksft_skip}"
218 fi
219
220 if [ ! -d "${NETCONS_CONFIGFS}" ]; then
221 echo "SKIP: directory ${NETCONS_CONFIGFS} does not exist. Check if NETCONSOLE_DYNAMIC is enabled" >&2
222 exit "${ksft_skip}"
223 fi
224
225 if ip link show "${DSTIF}" 2> /dev/null; then
226 echo "SKIP: interface ${DSTIF} exists in the system. Not overwriting it." >&2
227 exit "${ksft_skip}"
228 fi
229
230 if ip addr list | grep -E "inet.*(${SRCIP}|${DSTIP})" 2> /dev/null; then
231 echo "SKIP: IPs already in use. Skipping it" >&2
232 exit "${ksft_skip}"
233 fi
234}
235
236# ========== #
237# Start here #
238# ========== #
239modprobe netdevsim 2> /dev/null || true
240modprobe netconsole 2> /dev/null || true
241
242# The content of kmsg will be save to the following file
243OUTPUT_FILE="/tmp/${TARGET}"
244
245# Check for basic system dependency and exit if not found
246check_for_dependencies
247# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
248echo "6 5" > /proc/sys/kernel/printk
249# Remove the namespace, interfaces and netconsole target on exit
250trap cleanup EXIT
251# Create one namespace and two interfaces
252set_network
253# Create a dynamic target for netconsole
254create_dynamic_target
255# Set userdata "key" with the "value" value
256set_user_data
257# Listed for netconsole port inside the namespace and destination interface
258listen_port_and_save_to "${OUTPUT_FILE}" &
259# Wait for socat to start and listen to the port.
260wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
261# Send the message
262echo "${MSG}: ${TARGET}" > /dev/kmsg
263# Wait until socat saves the file to disk
264busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
265
266# Make sure the message was received in the dst part
267# and exit
268validate_result "${OUTPUT_FILE}"