xref: /linux/tools/testing/selftests/drivers/net/netcons_basic.sh (revision 0ce92d548b44649a8de706f9bb9e74a4ed2f18a7)
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# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)
32echo "6 5" > /proc/sys/kernel/printk
33# Remove the namespace, interfaces and netconsole target on exit
34trap cleanup EXIT
35
36# Run the test twice, with different format modes
37for FORMAT in "basic" "extended"
38do
39	echo "Running with target mode: ${FORMAT}"
40	# Create one namespace and two interfaces
41	set_network
42	# Create a dynamic target for netconsole
43	create_dynamic_target "${FORMAT}"
44	# Only set userdata for extended format
45	if [ "$FORMAT" == "extended" ]
46	then
47		# Set userdata "key" with the "value" value
48		set_user_data
49	fi
50	# Listed for netconsole port inside the namespace and destination interface
51	listen_port_and_save_to "${OUTPUT_FILE}" &
52	# Wait for socat to start and listen to the port.
53	wait_local_port_listen "${NAMESPACE}" "${PORT}" udp
54	# Send the message
55	echo "${MSG}: ${TARGET}" > /dev/kmsg
56	# Wait until socat saves the file to disk
57	busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
58
59	# Make sure the message was received in the dst part
60	# and exit
61	validate_result "${OUTPUT_FILE}" "${FORMAT}"
62	cleanup
63done
64
65trap - EXIT
66exit "${ksft_pass}"
67