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
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>
19 SCRIPTDIR
=$
(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
21 # Simple script to test dynamic targets in netconsole
22 SRCIF
="" # to be populated later
24 DSTIF
="" # to be populated later
28 MSG
="netconsole selftest"
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
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
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 {} \
;)
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
79 echo "linking netdevsim1 with netdevsim2 should succeed"
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
98 # Create both interfaces, and assign the destination to a different
102 # Link both interfaces back to back
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
124 local NSIM_DEV_SYS_DEL
="/sys/bus/netdevsim/del_device"
126 # delete netconsole dynamic reconfiguration
127 echo 0 > "${NETCONS_PATH}"/enabled
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
140 # Restoring printk configurations
141 echo "${DEFAULT_PRINTK_VALUES}" > /proc
/sys
/kernel
/printk
144 function set_user_data
() {
145 if [[ ! -d "${NETCONS_PATH}""/userdata" ]]
147 echo "Userdata path not available in ${NETCONS_PATH}/userdata"
151 mkdir
-p "${KEY_PATH}"
152 VALUE_PATH
="${KEY_PATH}""/value"
153 echo "${USERDATA_VALUE}" > "${VALUE_PATH}"
156 function listen_port_and_save_to
() {
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
170 # Check if the file exists
171 if [ ! -f "$TMPFILENAME" ]; then
172 echo "FAIL: File was not generated." >&2
176 if ! grep -q "${MSG}" "${TMPFILENAME}"; then
177 echo "FAIL: ${MSG} not found in ${TMPFILENAME}" >&2
178 cat "${TMPFILENAME}" >&2
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
188 # Delete the file once it is validated, otherwise keep it
189 # for debugging purposes
194 function check_for_dependencies
() {
195 if [ "$(id -u)" -ne 0 ]; then
196 echo "This test must be run as root" >&2
200 if ! which socat
> /dev
/null
; then
201 echo "SKIP: socat(1) is not available" >&2
205 if ! which ip
> /dev
/null
; then
206 echo "SKIP: ip(1) is not available" >&2
210 if ! which udevadm
> /dev
/null
; then
211 echo "SKIP: udevadm(1) is not available" >&2
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
220 if [ ! -d "${NETCONS_CONFIGFS}" ]; then
221 echo "SKIP: directory ${NETCONS_CONFIGFS} does not exist. Check if NETCONSOLE_DYNAMIC is enabled" >&2
225 if ip link show
"${DSTIF}" 2> /dev
/null
; then
226 echo "SKIP: interface ${DSTIF} exists in the system. Not overwriting it." >&2
230 if ip addr list |
grep -E "inet.*(${SRCIP}|${DSTIP})" 2> /dev
/null
; then
231 echo "SKIP: IPs already in use. Skipping it" >&2
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
251 # Create one namespace and two interfaces
253 # Create a dynamic target for netconsole
254 create_dynamic_target
255 # Set userdata "key" with the "value" value
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
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
268 validate_result
"${OUTPUT_FILE}"