xref: /linux/tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh (revision 359bcf15ec1d6738ede721db628594ecf05fd998)
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 <leitao@debian.org>
8
9set -euo pipefail
10
11LIBDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
12
13SRCIF="" # to be populated later
14SRCIP=192.0.2.1
15DSTIF="" # to be populated later
16DSTIP=192.0.2.2
17
18PORT="6666"
19MSG="netconsole selftest"
20USERDATA_KEY="key"
21USERDATA_VALUE="value"
22TARGET=$(mktemp -u netcons_XXXXX)
23DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/printk)
24NETCONS_CONFIGFS="/sys/kernel/config/netconsole"
25NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"
26# NAMESPACE will be populated by setup_ns with a random value
27NAMESPACE=""
28
29# IDs for netdevsim
30NSIM_DEV_1_ID=$((256 + RANDOM % 256))
31NSIM_DEV_2_ID=$((512 + RANDOM % 256))
32NSIM_DEV_SYS_NEW="/sys/bus/netdevsim/new_device"
33
34# Used to create and delete namespaces
35source "${LIBDIR}"/../../../../net/lib.sh
36
37# Create netdevsim interfaces
38create_ifaces() {
39
40	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW"
41	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW"
42	udevadm settle 2> /dev/null || true
43
44	local NSIM1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_1_ID"
45	local NSIM2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_2_ID"
46
47	# These are global variables
48	SRCIF=$(find "$NSIM1"/net -maxdepth 1 -type d ! \
49		-path "$NSIM1"/net -exec basename {} \;)
50	DSTIF=$(find "$NSIM2"/net -maxdepth 1 -type d ! \
51		-path "$NSIM2"/net -exec basename {} \;)
52}
53
54link_ifaces() {
55	local NSIM_DEV_SYS_LINK="/sys/bus/netdevsim/link_device"
56	local SRCIF_IFIDX=$(cat /sys/class/net/"$SRCIF"/ifindex)
57	local DSTIF_IFIDX=$(cat /sys/class/net/"$DSTIF"/ifindex)
58
59	exec {NAMESPACE_FD}</var/run/netns/"${NAMESPACE}"
60	exec {INITNS_FD}</proc/self/ns/net
61
62	# Bind the dst interface to namespace
63	ip link set "${DSTIF}" netns "${NAMESPACE}"
64
65	# Linking one device to the other one (on the other namespace}
66	if ! echo "${INITNS_FD}:$SRCIF_IFIDX $NAMESPACE_FD:$DSTIF_IFIDX"  > $NSIM_DEV_SYS_LINK
67	then
68		echo "linking netdevsim1 with netdevsim2 should succeed"
69		cleanup
70		exit "${ksft_skip}"
71	fi
72}
73
74function configure_ip() {
75	# Configure the IPs for both interfaces
76	ip netns exec "${NAMESPACE}" ip addr add "${DSTIP}"/24 dev "${DSTIF}"
77	ip netns exec "${NAMESPACE}" ip link set "${DSTIF}" up
78
79	ip addr add "${SRCIP}"/24 dev "${SRCIF}"
80	ip link set "${SRCIF}" up
81}
82
83function set_network() {
84	# setup_ns function is coming from lib.sh
85	setup_ns NAMESPACE
86
87	# Create both interfaces, and assign the destination to a different
88	# namespace
89	create_ifaces
90
91	# Link both interfaces back to back
92	link_ifaces
93
94	configure_ip
95}
96
97function create_dynamic_target() {
98	local FORMAT=${1:-"extended"}
99
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	if [ "${FORMAT}" == "basic" ]
112	then
113		# Basic target does not support release
114		echo 0 > "${NETCONS_PATH}"/release
115		echo 0 > "${NETCONS_PATH}"/extended
116	elif [ "${FORMAT}" == "extended" ]
117	then
118		echo 1 > "${NETCONS_PATH}"/extended
119	fi
120
121	echo 1 > "${NETCONS_PATH}"/enabled
122}
123
124# Do not append the release to the header of the message
125function disable_release_append() {
126	echo 0 > "${NETCONS_PATH}"/enabled
127	echo 0 > "${NETCONS_PATH}"/release
128	echo 1 > "${NETCONS_PATH}"/enabled
129}
130
131function cleanup() {
132	local NSIM_DEV_SYS_DEL="/sys/bus/netdevsim/del_device"
133
134	# delete netconsole dynamic reconfiguration
135	echo 0 > "${NETCONS_PATH}"/enabled
136	# Remove all the keys that got created during the selftest
137	find "${NETCONS_PATH}/userdata/" -mindepth 1 -type d -delete
138	# Remove the configfs entry
139	rmdir "${NETCONS_PATH}"
140
141	# Delete netdevsim devices
142	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL"
143	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL"
144
145	# this is coming from lib.sh
146	cleanup_all_ns
147
148	# Restoring printk configurations
149	echo "${DEFAULT_PRINTK_VALUES}" > /proc/sys/kernel/printk
150}
151
152function set_user_data() {
153	if [[ ! -d "${NETCONS_PATH}""/userdata" ]]
154	then
155		echo "Userdata path not available in ${NETCONS_PATH}/userdata"
156		exit "${ksft_skip}"
157	fi
158
159	KEY_PATH="${NETCONS_PATH}/userdata/${USERDATA_KEY}"
160	mkdir -p "${KEY_PATH}"
161	VALUE_PATH="${KEY_PATH}""/value"
162	echo "${USERDATA_VALUE}" > "${VALUE_PATH}"
163}
164
165function listen_port_and_save_to() {
166	local OUTPUT=${1}
167	# Just wait for 2 seconds
168	timeout 2 ip netns exec "${NAMESPACE}" \
169		socat UDP-LISTEN:"${PORT}",fork "${OUTPUT}"
170}
171
172function validate_result() {
173	local TMPFILENAME="$1"
174	local FORMAT=${2:-"extended"}
175
176	# TMPFILENAME will contain something like:
177	# 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM
178	#  key=value
179
180	# Check if the file exists
181	if [ ! -f "$TMPFILENAME" ]; then
182		echo "FAIL: File was not generated." >&2
183		exit "${ksft_fail}"
184	fi
185
186	if ! grep -q "${MSG}" "${TMPFILENAME}"; then
187		echo "FAIL: ${MSG} not found in ${TMPFILENAME}" >&2
188		cat "${TMPFILENAME}" >&2
189		exit "${ksft_fail}"
190	fi
191
192	# userdata is not supported on basic format target,
193	# thus, do not validate it.
194	if [ "${FORMAT}" != "basic" ];
195	then
196		if ! grep -q "${USERDATA_KEY}=${USERDATA_VALUE}" "${TMPFILENAME}"; then
197			echo "FAIL: ${USERDATA_KEY}=${USERDATA_VALUE} not found in ${TMPFILENAME}" >&2
198			cat "${TMPFILENAME}" >&2
199			exit "${ksft_fail}"
200		fi
201	fi
202
203	# Delete the file once it is validated, otherwise keep it
204	# for debugging purposes
205	rm "${TMPFILENAME}"
206}
207
208function check_for_dependencies() {
209	if [ "$(id -u)" -ne 0 ]; then
210		echo "This test must be run as root" >&2
211		exit "${ksft_skip}"
212	fi
213
214	if ! which socat > /dev/null ; then
215		echo "SKIP: socat(1) is not available" >&2
216		exit "${ksft_skip}"
217	fi
218
219	if ! which ip > /dev/null ; then
220		echo "SKIP: ip(1) is not available" >&2
221		exit "${ksft_skip}"
222	fi
223
224	if ! which udevadm > /dev/null ; then
225		echo "SKIP: udevadm(1) is not available" >&2
226		exit "${ksft_skip}"
227	fi
228
229	if [ ! -f "${NSIM_DEV_SYS_NEW}" ]; then
230		echo "SKIP: file ${NSIM_DEV_SYS_NEW} does not exist. Check if CONFIG_NETDEVSIM is enabled" >&2
231		exit "${ksft_skip}"
232	fi
233
234	if [ ! -d "${NETCONS_CONFIGFS}" ]; then
235		echo "SKIP: directory ${NETCONS_CONFIGFS} does not exist. Check if NETCONSOLE_DYNAMIC is enabled" >&2
236		exit "${ksft_skip}"
237	fi
238
239	if ip link show "${DSTIF}" 2> /dev/null; then
240		echo "SKIP: interface ${DSTIF} exists in the system. Not overwriting it." >&2
241		exit "${ksft_skip}"
242	fi
243
244	if ip addr list | grep -E "inet.*(${SRCIP}|${DSTIP})" 2> /dev/null; then
245		echo "SKIP: IPs already in use. Skipping it" >&2
246		exit "${ksft_skip}"
247	fi
248}
249
250function check_for_taskset() {
251	if ! which taskset > /dev/null ; then
252		echo "SKIP: taskset(1) is not available" >&2
253		exit "${ksft_skip}"
254	fi
255}
256
257# This is necessary if running multiple tests in a row
258function pkill_socat() {
259	PROCESS_NAME="socat UDP-LISTEN:6666,fork ${OUTPUT_FILE}"
260	# socat runs under timeout(1), kill it if it is still alive
261	# do not fail if socat doesn't exist anymore
262	set +e
263	pkill -f "${PROCESS_NAME}"
264	set -e
265}
266