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.168.1.1
24DSTIF="" # to be populated later
25DSTIP=192.168.1.2
26
27PORT="6666"
28MSG="netconsole selftest"
29TARGET=$(mktemp -u netcons_XXXXX)
30DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/printk)
31NETCONS_CONFIGFS="/sys/kernel/config/netconsole"
32NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"
33# NAMESPACE will be populated by setup_ns with a random value
34NAMESPACE=""
35
36# IDs for netdevsim
37NSIM_DEV_1_ID=$((256 + RANDOM % 256))
38NSIM_DEV_2_ID=$((512 + RANDOM % 256))
39
40# Used to create and delete namespaces
41source "${SCRIPTDIR}"/../../net/lib.sh
42source "${SCRIPTDIR}"/../../net/net_helper.sh
43
44# Create netdevsim interfaces
45create_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
62link_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
82function 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
91function 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
105function 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
120function 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
139function 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
146function 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
167function 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# ========== #
207modprobe netdevsim 2> /dev/null || true
208modprobe netconsole 2> /dev/null || true
209
210# The content of kmsg will be save to the following file
211OUTPUT_FILE="/tmp/${TARGET}"
212
213# Check for basic system dependency and exit if not found
214check_for_dependencies
215# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
216echo "6 5" > /proc/sys/kernel/printk
217# Remove the namespace, interfaces and netconsole target on exit
218trap cleanup EXIT
219# Create one namespace and two interfaces
220set_network
221# Create a dynamic target for netconsole
222create_dynamic_target
223# Listed for netconsole port inside the namespace and destination interface
224listen_port_and_save_to "${OUTPUT_FILE}" &
225# Wait for socat to start and listen to the port.
226wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
227# Send the message
228echo "${MSG}: ${TARGET}" > /dev/kmsg
229# Wait until socat saves the file to disk
230busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
231
232# Make sure the message was received in the dst part
233# and exit
234validate_result "${OUTPUT_FILE}"
235