Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / drivers / net / netcons_basic.sh
blobb175f4d966e5056ddb62e335f212c03e55f50fb0
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: GPL-2.0
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.
8 # It listens on the destination interface using socat and configures a dynamic
9 # target on netconsole, pointing to the destination IP address.
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.
15 # Author: Breno Leitao <leitao@debian.org>
17 set -euo pipefail
19 SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
21 # Simple script to test dynamic targets in netconsole
22 SRCIF="" # to be populated later
23 SRCIP=192.0.2.1
24 DSTIF="" # to be populated later
25 DSTIP=192.0.2.2
27 PORT="6666"
28 MSG="netconsole selftest"
29 USERDATA_KEY="key"
30 USERDATA_VALUE="value"
31 TARGET=$(mktemp -u netcons_XXXXX)
32 DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/printk)
33 NETCONS_CONFIGFS="/sys/kernel/config/netconsole"
34 NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"
35 KEY_PATH="${NETCONS_PATH}/userdata/${USERDATA_KEY}"
36 # NAMESPACE will be populated by setup_ns with a random value
37 NAMESPACE=""
39 # IDs for netdevsim
40 NSIM_DEV_1_ID=$((256 + RANDOM % 256))
41 NSIM_DEV_2_ID=$((512 + RANDOM % 256))
42 NSIM_DEV_SYS_NEW="/sys/bus/netdevsim/new_device"
44 # Used to create and delete namespaces
45 source "${SCRIPTDIR}"/../../net/lib.sh
46 source "${SCRIPTDIR}"/../../net/net_helper.sh
48 # Create netdevsim interfaces
49 create_ifaces() {
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
55 local NSIM1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_1_ID"
56 local NSIM2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_2_ID"
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 {} \;)
65 link_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)
70 exec {NAMESPACE_FD}</var/run/netns/"${NAMESPACE}"
71 exec {INITNS_FD}</proc/self/ns/net
73 # Bind the dst interface to namespace
74 ip link set "${DSTIF}" netns "${NAMESPACE}"
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}"
85 function 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
90 ip addr add "${SRCIP}"/24 dev "${SRCIF}"
91 ip link set "${SRCIF}" up
94 function set_network() {
95 # setup_ns function is coming from lib.sh
96 setup_ns NAMESPACE
98 # Create both interfaces, and assign the destination to a different
99 # namespace
100 create_ifaces
102 # Link both interfaces back to back
103 link_ifaces
105 configure_ip
108 function create_dynamic_target() {
109 DSTMAC=$(ip netns exec "${NAMESPACE}" \
110 ip link show "${DSTIF}" | awk '/ether/ {print $2}')
112 # Create a dynamic target
113 mkdir "${NETCONS_PATH}"
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
120 echo 1 > "${NETCONS_PATH}"/enabled
123 function cleanup() {
124 local NSIM_DEV_SYS_DEL="/sys/bus/netdevsim/del_device"
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}"
133 # Delete netdevsim devices
134 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL"
135 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL"
137 # this is coming from lib.sh
138 cleanup_all_ns
140 # Restoring printk configurations
141 echo "${DEFAULT_PRINTK_VALUES}" > /proc/sys/kernel/printk
144 function 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}"
151 mkdir -p "${KEY_PATH}"
152 VALUE_PATH="${KEY_PATH}""/value"
153 echo "${USERDATA_VALUE}" > "${VALUE_PATH}"
156 function 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}"
163 function validate_result() {
164 local TMPFILENAME="$1"
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
170 # Check if the file exists
171 if [ ! -f "$TMPFILENAME" ]; then
172 echo "FAIL: File was not generated." >&2
173 exit "${ksft_fail}"
176 if ! grep -q "${MSG}" "${TMPFILENAME}"; then
177 echo "FAIL: ${MSG} not found in ${TMPFILENAME}" >&2
178 cat "${TMPFILENAME}" >&2
179 exit "${ksft_fail}"
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}"
188 # Delete the file once it is validated, otherwise keep it
189 # for debugging purposes
190 rm "${TMPFILENAME}"
191 exit "${ksft_pass}"
194 function check_for_dependencies() {
195 if [ "$(id -u)" -ne 0 ]; then
196 echo "This test must be run as root" >&2
197 exit "${ksft_skip}"
200 if ! which socat > /dev/null ; then
201 echo "SKIP: socat(1) is not available" >&2
202 exit "${ksft_skip}"
205 if ! which ip > /dev/null ; then
206 echo "SKIP: ip(1) is not available" >&2
207 exit "${ksft_skip}"
210 if ! which udevadm > /dev/null ; then
211 echo "SKIP: udevadm(1) is not available" >&2
212 exit "${ksft_skip}"
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}"
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}"
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}"
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}"
236 # ========== #
237 # Start here #
238 # ========== #
239 modprobe netdevsim 2> /dev/null || true
240 modprobe netconsole 2> /dev/null || true
242 # The content of kmsg will be save to the following file
243 OUTPUT_FILE="/tmp/${TARGET}"
245 # Check for basic system dependency and exit if not found
246 check_for_dependencies
247 # Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
248 echo "6 5" > /proc/sys/kernel/printk
249 # Remove the namespace, interfaces and netconsole target on exit
250 trap cleanup EXIT
251 # Create one namespace and two interfaces
252 set_network
253 # Create a dynamic target for netconsole
254 create_dynamic_target
255 # Set userdata "key" with the "value" value
256 set_user_data
257 # Listed for netconsole port inside the namespace and destination interface
258 listen_port_and_save_to "${OUTPUT_FILE}" &
259 # Wait for socat to start and listen to the port.
260 wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
261 # Send the message
262 echo "${MSG}: ${TARGET}" > /dev/kmsg
263 # Wait until socat saves the file to disk
264 busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
266 # Make sure the message was received in the dst part
267 # and exit
268 validate_result "${OUTPUT_FILE}"