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