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	for IP_VERSION in "ipv6" "ipv4"
40	do
41		echo "Running with target mode: ${FORMAT} (${IP_VERSION})"
42		# Create one namespace and two interfaces
43		set_network "${IP_VERSION}"
44		# Create a dynamic target for netconsole
45		create_dynamic_target "${FORMAT}"
46		# Only set userdata for extended format
47		if [ "$FORMAT" == "extended" ]
48		then
49			# Set userdata "key" with the "value" value
50			set_user_data
51		fi
52		# Listed for netconsole port inside the namespace and
53		# destination interface
54		listen_port_and_save_to "${OUTPUT_FILE}" "${IP_VERSION}" &
55		# Wait for socat to start and listen to the port.
56		wait_for_port "${NAMESPACE}" "${PORT}" "${IP_VERSION}"
57		# Send the message
58		echo "${MSG}: ${TARGET}" > /dev/kmsg
59		# Wait until socat saves the file to disk
60		busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"
61
62		# Make sure the message was received in the dst part
63		# and exit
64		validate_result "${OUTPUT_FILE}" "${FORMAT}"
65		# kill socat in case it is still running
66		pkill_socat
67		cleanup
68		echo "${FORMAT} : ${IP_VERSION} : Test passed" >&2
69	done
70done
71
72trap - EXIT
73exit "${ksft_pass}"
74