xref: /linux/tools/testing/selftests/drivers/net/netconsole/netcons_resume.sh (revision 37a93dd5c49b5fda807fd204edf2547c3493319c)
1#!/usr/bin/env bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test validates that netconsole is able to resume a target that was
5# deactivated when its interface was removed when the interface is brought
6# back up.
7#
8# The test configures a netconsole target and then removes netdevsim module to
9# cause the interface to disappear. Targets are configured via cmdline to ensure
10# targets bound by interface name and mac address can be resumed.
11# The test verifies that the target moved to disabled state before adding
12# netdevsim and the interface back.
13#
14# Finally, the test verifies that the target is re-enabled automatically and
15# the message is received on the destination interface.
16#
17# Author: Andre Carvalho <asantostc@gmail.com>
18
19set -euo pipefail
20
21SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
22
23source "${SCRIPTDIR}"/../lib/sh/lib_netcons.sh
24
25SAVED_SRCMAC="" # to be populated later
26SAVED_DSTMAC="" # to be populated later
27
28modprobe netdevsim 2> /dev/null || true
29rmmod netconsole 2> /dev/null || true
30
31check_netconsole_module
32
33function cleanup() {
34	cleanup_netcons "${NETCONS_CONFIGFS}/cmdline0"
35	do_cleanup
36	rmmod netconsole
37}
38
39function trigger_reactivation() {
40	# Add back low level module
41	modprobe netdevsim
42	# Recreate namespace and two interfaces
43	set_network
44	# Restore MACs
45	ip netns exec "${NAMESPACE}" ip link set "${DSTIF}" \
46		address "${SAVED_DSTMAC}"
47	if [ "${BINDMODE}" == "mac" ]; then
48		ip link set dev "${SRCIF}" down
49		ip link set dev "${SRCIF}" address "${SAVED_SRCMAC}"
50		# Rename device in order to trigger target resume, as initial
51		# when device was recreated it didn't have correct mac address.
52		ip link set dev "${SRCIF}" name "${TARGET}"
53	fi
54}
55
56function trigger_deactivation() {
57	# Start by storing mac addresses so we can be restored in reactivate
58	SAVED_DSTMAC=$(ip netns exec "${NAMESPACE}" \
59		cat /sys/class/net/"$DSTIF"/address)
60	SAVED_SRCMAC=$(mac_get "${SRCIF}")
61	# Remove low level module
62	rmmod netdevsim
63}
64
65trap cleanup EXIT
66
67# Run the test twice, with different cmdline parameters
68for BINDMODE in "ifname" "mac"
69do
70	echo "Running with bind mode: ${BINDMODE}" >&2
71	# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
72	echo "6 5" > /proc/sys/kernel/printk
73
74	# Create one namespace and two interfaces
75	set_network
76
77	# Create the command line for netconsole, with the configuration from
78	# the function above
79	CMDLINE=$(create_cmdline_str "${BINDMODE}")
80
81	# The content of kmsg will be save to the following file
82	OUTPUT_FILE="/tmp/${TARGET}-${BINDMODE}"
83
84	# Load the module, with the cmdline set
85	modprobe netconsole "${CMDLINE}"
86	# Expose cmdline target in configfs
87	mkdir "${NETCONS_CONFIGFS}/cmdline0"
88
89	# Target should be enabled
90	wait_target_state "cmdline0" "enabled"
91
92	# Trigger deactivation by unloading netdevsim module. Target should be
93	# disabled.
94	trigger_deactivation
95	wait_target_state "cmdline0" "disabled"
96
97	# Trigger reactivation by loading netdevsim, recreating the network and
98	# restoring mac addresses. Target should be re-enabled.
99	trigger_reactivation
100	wait_target_state "cmdline0" "enabled"
101
102	# Listen for netconsole port inside the namespace and destination
103	# interface
104	listen_port_and_save_to "${OUTPUT_FILE}" &
105	# Wait for socat to start and listen to the port.
106	wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
107	# Send the message
108	echo "${MSG}: ${TARGET}" > /dev/kmsg
109	# Wait until socat saves the file to disk
110	busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
111	# Make sure the message was received in the dst part
112	# and exit
113	validate_msg "${OUTPUT_FILE}"
114
115	# kill socat in case it is still running
116	pkill_socat
117	# Cleanup & unload the module
118	cleanup
119
120	echo "${BINDMODE} : Test passed" >&2
121done
122
123trap - EXIT
124exit "${EXIT_STATUS}"
125