xref: /linux/tools/testing/selftests/drivers/net/netconsole/netcons_resume.sh (revision fafb66e5903c2bcfc7b7e259042a8282f18a6faa)
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" ] &&
48		[ "$(mac_get "${SRCIF}")" != "${SAVED_SRCMAC}" ]; then
49		ip link set dev "${SRCIF}" down
50		ip link set dev "${SRCIF}" address "${SAVED_SRCMAC}"
51		# Rename device in order to trigger target resume, as initial
52		# when device was recreated it didn't have correct mac address.
53		ip link set dev "${SRCIF}" name "${TARGET}"
54	fi
55}
56
57function trigger_deactivation() {
58	# Start by storing mac addresses so we can be restored in reactivate
59	SAVED_DSTMAC=$(ip netns exec "${NAMESPACE}" \
60		cat /sys/class/net/"$DSTIF"/address)
61	SAVED_SRCMAC=$(mac_get "${SRCIF}")
62	# Remove low level module
63	rmmod netdevsim
64}
65
66trap cleanup EXIT
67
68# Run the test twice, with different cmdline parameters
69for BINDMODE in "ifname" "mac"
70do
71	echo "Running with bind mode: ${BINDMODE}" >&2
72	# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
73	echo "6 5" > /proc/sys/kernel/printk
74
75	# Create one namespace and two interfaces
76	set_network
77
78	# Create the command line for netconsole, with the configuration from
79	# the function above
80	CMDLINE=$(create_cmdline_str "${BINDMODE}")
81
82	# The content of kmsg will be save to the following file
83	OUTPUT_FILE="/tmp/${TARGET}-${BINDMODE}"
84
85	# Load the module, with the cmdline set
86	modprobe netconsole "${CMDLINE}"
87	# Expose cmdline target in configfs
88	mkdir "${NETCONS_CONFIGFS}/cmdline0"
89
90	# Target should be enabled
91	wait_target_state "cmdline0" "enabled"
92
93	# Trigger deactivation by unloading netdevsim module. Target should be
94	# disabled.
95	trigger_deactivation
96	wait_target_state "cmdline0" "disabled"
97
98	# Trigger reactivation by loading netdevsim, recreating the network and
99	# restoring mac addresses. Target should be re-enabled.
100	trigger_reactivation
101	wait_target_state "cmdline0" "enabled"
102
103	# Listen for netconsole port inside the namespace and destination
104	# interface
105	listen_port_and_save_to "${OUTPUT_FILE}" &
106	# Wait for socat to start and listen to the port.
107	wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
108	# Send the message
109	echo "${MSG}: ${TARGET}" > /dev/kmsg
110	# Wait until socat saves the file to disk
111	busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
112	# Make sure the message was received in the dst part
113	# and exit
114	validate_msg "${OUTPUT_FILE}"
115
116	# kill socat in case it is still running
117	pkill_socat
118	# Cleanup & unload the module
119	cleanup
120
121	echo "${BINDMODE} : Test passed" >&2
122done
123
124trap - EXIT
125exit "${EXIT_STATUS}"
126