Path: blob/master/tools/testing/selftests/drivers/net/netcons_basic.sh
26288 views
#!/usr/bin/env bash1# SPDX-License-Identifier: GPL-2.023# This test creates two netdevsim virtual interfaces, assigns one of them (the4# "destination interface") to a new namespace, and assigns IP addresses to both5# interfaces.6#7# It listens on the destination interface using socat and configures a dynamic8# target on netconsole, pointing to the destination IP address.9#10# Finally, it checks whether the message was received properly on the11# destination interface. Note that this test may pollute the kernel log buffer12# (dmesg) and relies on dynamic configuration and namespaces being configured.13#14# Author: Breno Leitao <[email protected]>1516set -euo pipefail1718SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")1920source "${SCRIPTDIR}"/lib/sh/lib_netcons.sh2122modprobe netdevsim 2> /dev/null || true23modprobe netconsole 2> /dev/null || true2425# The content of kmsg will be save to the following file26OUTPUT_FILE="/tmp/${TARGET}"2728# Check for basic system dependency and exit if not found29check_for_dependencies30# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)31echo "6 5" > /proc/sys/kernel/printk32# Remove the namespace, interfaces and netconsole target on exit33trap cleanup EXIT3435# Run the test twice, with different format modes36for FORMAT in "basic" "extended"37do38for IP_VERSION in "ipv6" "ipv4"39do40echo "Running with target mode: ${FORMAT} (${IP_VERSION})"41# Create one namespace and two interfaces42set_network "${IP_VERSION}"43# Create a dynamic target for netconsole44create_dynamic_target "${FORMAT}"45# Only set userdata for extended format46if [ "$FORMAT" == "extended" ]47then48# Set userdata "key" with the "value" value49set_user_data50fi51# Listed for netconsole port inside the namespace and52# destination interface53listen_port_and_save_to "${OUTPUT_FILE}" "${IP_VERSION}" &54# Wait for socat to start and listen to the port.55wait_for_port "${NAMESPACE}" "${PORT}" "${IP_VERSION}"56# Send the message57echo "${MSG}: ${TARGET}" > /dev/kmsg58# Wait until socat saves the file to disk59busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"6061# Make sure the message was received in the dst part62# and exit63validate_result "${OUTPUT_FILE}" "${FORMAT}"64# kill socat in case it is still running65pkill_socat66cleanup67echo "${FORMAT} : ${IP_VERSION} : Test passed" >&268done69done7071trap - EXIT72exit "${ksft_pass}"737475