xref: /linux/tools/testing/selftests/drivers/net/netcons_basic.sh (revision 24f171c7e145f43b9f187578e89b0982ce87e54c)
1#!/usr/bin/env bash
2# SPDX-License-Identifier: GPL-2.0
3
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
6# interfaces.
7#
8# It listens on the destination interface using socat and configures a dynamic
9# target on netconsole, pointing to the destination IP address.
10#
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.
14#
15# Author: Breno Leitao <leitao@debian.org>
16
17set -euo pipefail
18
19SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
20
21source "${SCRIPTDIR}"/lib/sh/lib_netcons.sh
22
23modprobe netdevsim 2> /dev/null || true
24modprobe netconsole 2> /dev/null || true
25
26# The content of kmsg will be save to the following file
27OUTPUT_FILE="/tmp/${TARGET}"
28
29# Check for basic system dependency and exit if not found
30check_for_dependencies
31# Remove the namespace, interfaces and netconsole target on exit
32trap cleanup EXIT
33
34# Run the test twice, with different format modes
35for FORMAT in "basic" "extended"
36do
37	for IP_VERSION in "ipv6" "ipv4"
38	do
39		echo "Running with target mode: ${FORMAT} (${IP_VERSION})"
40		# Set current loglevel to KERN_INFO(6), and default to
41		# KERN_NOTICE(5)
42		echo "6 5" > /proc/sys/kernel/printk
43		# Create one namespace and two interfaces
44		set_network "${IP_VERSION}"
45		# Create a dynamic target for netconsole
46		create_dynamic_target "${FORMAT}"
47		# Only set userdata for extended format
48		if [ "$FORMAT" == "extended" ]
49		then
50			# Set userdata "key" with the "value" value
51			set_user_data
52		fi
53		# Listed for netconsole port inside the namespace and
54		# destination interface
55		listen_port_and_save_to "${OUTPUT_FILE}" "${IP_VERSION}" &
56		# Wait for socat to start and listen to the port.
57		wait_for_port "${NAMESPACE}" "${PORT}" "${IP_VERSION}"
58		# Send the message
59		echo "${MSG}: ${TARGET}" > /dev/kmsg
60		# Wait until socat saves the file to disk
61		busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
62
63		# Make sure the message was received in the dst part
64		# and exit
65		validate_result "${OUTPUT_FILE}" "${FORMAT}"
66		# kill socat in case it is still running
67		pkill_socat
68		cleanup
69		echo "${FORMAT} : ${IP_VERSION} : Test passed" >&2
70	done
71done
72
73trap - EXIT
74exit "${ksft_pass}"
75