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 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
36source "${LIBDIR}"/../../../../net/net_helper.sh
37
38# Create netdevsim interfaces
39create_ifaces() {
40
41 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW"
42 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW"
43 udevadm settle 2> /dev/null || true
44
45 local NSIM1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_1_ID"
46 local NSIM2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_2_ID"
47
48 # These are global variables
49 SRCIF=$(find "$NSIM1"/net -maxdepth 1 -type d ! \
50 -path "$NSIM1"/net -exec basename {} \;)
51 DSTIF=$(find "$NSIM2"/net -maxdepth 1 -type d ! \
52 -path "$NSIM2"/net -exec basename {} \;)
53}
54
55link_ifaces() {
56 local NSIM_DEV_SYS_LINK="/sys/bus/netdevsim/link_device"
57 local SRCIF_IFIDX=$(cat /sys/class/net/"$SRCIF"/ifindex)
58 local DSTIF_IFIDX=$(cat /sys/class/net/"$DSTIF"/ifindex)
59
60 exec {NAMESPACE_FD}</var/run/netns/"${NAMESPACE}"
61 exec {INITNS_FD}</proc/self/ns/net
62
63 # Bind the dst interface to namespace
64 ip link set "${DSTIF}" netns "${NAMESPACE}"
65
66 # Linking one device to the other one (on the other namespace}
67 if ! echo "${INITNS_FD}:$SRCIF_IFIDX $NAMESPACE_FD:$DSTIF_IFIDX" > $NSIM_DEV_SYS_LINK
68 then
69 echo "linking netdevsim1 with netdevsim2 should succeed"
70 cleanup
71 exit "${ksft_skip}"
72 fi
73}
74
75function configure_ip() {
76 # Configure the IPs for both interfaces
77 ip netns exec "${NAMESPACE}" ip addr add "${DSTIP}"/24 dev "${DSTIF}"
78 ip netns exec "${NAMESPACE}" ip link set "${DSTIF}" up
79
80 ip addr add "${SRCIP}"/24 dev "${SRCIF}"
81 ip link set "${SRCIF}" up
82}
83
84function set_network() {
85 # setup_ns function is coming from lib.sh
86 setup_ns NAMESPACE
87
88 # Create both interfaces, and assign the destination to a different
89 # namespace
90 create_ifaces
91
92 # Link both interfaces back to back
93 link_ifaces
94
95 configure_ip
96}
97
98function create_dynamic_target() {
99 DSTMAC=$(ip netns exec "${NAMESPACE}" \
100 ip link show "${DSTIF}" | awk '/ether/ {print $2}')
101
102 # Create a dynamic target
103 mkdir "${NETCONS_PATH}"
104
105 echo "${DSTIP}" > "${NETCONS_PATH}"/remote_ip
106 echo "${SRCIP}" > "${NETCONS_PATH}"/local_ip
107 echo "${DSTMAC}" > "${NETCONS_PATH}"/remote_mac
108 echo "${SRCIF}" > "${NETCONS_PATH}"/dev_name
109
110 echo 1 > "${NETCONS_PATH}"/enabled
111}
112
113# Do not append the release to the header of the message
114function disable_release_append() {
115 echo 0 > "${NETCONS_PATH}"/enabled
116 echo 0 > "${NETCONS_PATH}"/release
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 all the keys that got created during the selftest
126 find "${NETCONS_PATH}/userdata/" -mindepth 1 -type d -delete
127 # Remove the configfs entry
128 rmdir "${NETCONS_PATH}"
129
130 # Delete netdevsim devices
131 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL"
132 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL"
133
134 # this is coming from lib.sh
135 cleanup_all_ns
136
137 # Restoring printk configurations
138 echo "${DEFAULT_PRINTK_VALUES}" > /proc/sys/kernel/printk
139}
140
141function set_user_data() {
142 if [[ ! -d "${NETCONS_PATH}""/userdata" ]]
143 then
144 echo "Userdata path not available in ${NETCONS_PATH}/userdata"
145 exit "${ksft_skip}"
146 fi
147
148 KEY_PATH="${NETCONS_PATH}/userdata/${USERDATA_KEY}"
149 mkdir -p "${KEY_PATH}"
150 VALUE_PATH="${KEY_PATH}""/value"
151 echo "${USERDATA_VALUE}" > "${VALUE_PATH}"
152}
153
154function listen_port_and_save_to() {
155 local OUTPUT=${1}
156 # Just wait for 2 seconds
157 timeout 2 ip netns exec "${NAMESPACE}" \
158 socat UDP-LISTEN:"${PORT}",fork "${OUTPUT}"
159}
160
161function validate_result() {
162 local TMPFILENAME="$1"
163
164 # TMPFILENAME will contain something like:
165 # 6.11.1-0_fbk0_rc13_509_g30d75cea12f7,13,1822,115075213798,-;netconsole selftest: netcons_gtJHM
166 # key=value
167
168 # Check if the file exists
169 if [ ! -f "$TMPFILENAME" ]; then
170 echo "FAIL: File was not generated." >&2
171 exit "${ksft_fail}"
172 fi
173
174 if ! grep -q "${MSG}" "${TMPFILENAME}"; then
175 echo "FAIL: ${MSG} not found in ${TMPFILENAME}" >&2
176 cat "${TMPFILENAME}" >&2
177 exit "${ksft_fail}"
178 fi
179
180 if ! grep -q "${USERDATA_KEY}=${USERDATA_VALUE}" "${TMPFILENAME}"; then
181 echo "FAIL: ${USERDATA_KEY}=${USERDATA_VALUE} not found in ${TMPFILENAME}" >&2
182 cat "${TMPFILENAME}" >&2
183 exit "${ksft_fail}"
184 fi
185
186 # Delete the file once it is validated, otherwise keep it
187 # for debugging purposes
188 rm "${TMPFILENAME}"
189 exit "${ksft_pass}"
190}
191
192function check_for_dependencies() {
193 if [ "$(id -u)" -ne 0 ]; then
194 echo "This test must be run as root" >&2
195 exit "${ksft_skip}"
196 fi
197
198 if ! which socat > /dev/null ; then
199 echo "SKIP: socat(1) is not available" >&2
200 exit "${ksft_skip}"
201 fi
202
203 if ! which ip > /dev/null ; then
204 echo "SKIP: ip(1) is not available" >&2
205 exit "${ksft_skip}"
206 fi
207
208 if ! which udevadm > /dev/null ; then
209 echo "SKIP: udevadm(1) is not available" >&2
210 exit "${ksft_skip}"
211 fi
212
213 if [ ! -f "${NSIM_DEV_SYS_NEW}" ]; then
214 echo "SKIP: file ${NSIM_DEV_SYS_NEW} does not exist. Check if CONFIG_NETDEVSIM is enabled" >&2
215 exit "${ksft_skip}"
216 fi
217
218 if [ ! -d "${NETCONS_CONFIGFS}" ]; then
219 echo "SKIP: directory ${NETCONS_CONFIGFS} does not exist. Check if NETCONSOLE_DYNAMIC is enabled" >&2
220 exit "${ksft_skip}"
221 fi
222
223 if ip link show "${DSTIF}" 2> /dev/null; then
224 echo "SKIP: interface ${DSTIF} exists in the system. Not overwriting it." >&2
225 exit "${ksft_skip}"
226 fi
227
228 if ip addr list | grep -E "inet.*(${SRCIP}|${DSTIP})" 2> /dev/null; then
229 echo "SKIP: IPs already in use. Skipping it" >&2
230 exit "${ksft_skip}"
231 fi
232}
233
234function check_for_taskset() {
235 if ! which taskset > /dev/null ; then
236 echo "SKIP: taskset(1) is not available" >&2
237 exit "${ksft_skip}"
238 fi
239}
240
241# This is necessary if running multiple tests in a row
242function pkill_socat() {
243 PROCESS_NAME="socat UDP-LISTEN:6666,fork ${OUTPUT_FILE}"
244 # socat runs under timeout(1), kill it if it is still alive
245 # do not fail if socat doesn't exist anymore
246 set +e
247 pkill -f "${PROCESS_NAME}"
248 set -e
249}