summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/drivers/net/netcons_basic.sh
blob: 40a6ac6191b8b6d5c4279c7cfc31cc208ae998a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0

# This test creates two netdevsim virtual interfaces, assigns one of them (the
# "destination interface") to a new namespace, and assigns IP addresses to both
# interfaces.
#
# It listens on the destination interface using socat and configures a dynamic
# target on netconsole, pointing to the destination IP address.
#
# Finally, it checks whether the message was received properly on the
# destination interface.  Note that this test may pollute the kernel log buffer
# (dmesg) and relies on dynamic configuration and namespaces being configured.
#
# Author: Breno Leitao <leitao@debian.org>

set -euo pipefail

SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")

source "${SCRIPTDIR}"/lib/sh/lib_netcons.sh

modprobe netdevsim 2> /dev/null || true
modprobe netconsole 2> /dev/null || true

# The content of kmsg will be save to the following file
OUTPUT_FILE="/tmp/${TARGET}"

# Check for basic system dependency and exit if not found
check_for_dependencies
# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
echo "6 5" > /proc/sys/kernel/printk
# Remove the namespace, interfaces and netconsole target on exit
trap cleanup EXIT

# Run the test twice, with different format modes
for FORMAT in "basic" "extended"
do
	echo "Running with target mode: ${FORMAT}"
	# Create one namespace and two interfaces
	set_network
	# Create a dynamic target for netconsole
	create_dynamic_target "${FORMAT}"
	# Only set userdata for extended format
	if [ "$FORMAT" == "extended" ]
	then
		# Set userdata "key" with the "value" value
		set_user_data
	fi
	# Listed for netconsole port inside the namespace and destination interface
	listen_port_and_save_to "${OUTPUT_FILE}" &
	# Wait for socat to start and listen to the port.
	wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
	# Send the message
	echo "${MSG}: ${TARGET}" > /dev/kmsg
	# Wait until socat saves the file to disk
	busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"

	# Make sure the message was received in the dst part
	# and exit
	validate_result "${OUTPUT_FILE}" "${FORMAT}"
	cleanup
done

trap - EXIT
exit "${ksft_pass}"