brintos

brintos / linux-shallow public Read only

0
0
Text · 6.5 KiB · 06021b2 Raw
235 lines · bash
1#!/usr/bin/env bash2# SPDX-License-Identifier: GPL-2.03 4# This test creates two netdevsim virtual interfaces, assigns one of them (the5# "destination interface") to a new namespace, and assigns IP addresses to both6# interfaces.7#8# It listens on the destination interface using socat and configures a dynamic9# target on netconsole, pointing to the destination IP address.10#11# Finally, it checks whether the message was received properly on the12# destination interface.  Note that this test may pollute the kernel log buffer13# (dmesg) and relies on dynamic configuration and namespaces being configured.14#15# Author: Breno Leitao <leitao@debian.org>16 17set -euo pipefail18 19SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")20 21# Simple script to test dynamic targets in netconsole22SRCIF="" # to be populated later23SRCIP=192.168.1.124DSTIF="" # to be populated later25DSTIP=192.168.1.226 27PORT="6666"28MSG="netconsole selftest"29TARGET=$(mktemp -u netcons_XXXXX)30DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/printk)31NETCONS_CONFIGFS="/sys/kernel/config/netconsole"32NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"33# NAMESPACE will be populated by setup_ns with a random value34NAMESPACE=""35 36# IDs for netdevsim37NSIM_DEV_1_ID=$((256 + RANDOM % 256))38NSIM_DEV_2_ID=$((512 + RANDOM % 256))39 40# Used to create and delete namespaces41source "${SCRIPTDIR}"/../../net/lib.sh42source "${SCRIPTDIR}"/../../net/net_helper.sh43 44# Create netdevsim interfaces45create_ifaces() {46	local NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device47 48	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW"49	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW"50	udevadm settle 2> /dev/null || true51 52	local NSIM1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_1_ID"53	local NSIM2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_2_ID"54 55	# These are global variables56	SRCIF=$(find "$NSIM1"/net -maxdepth 1 -type d ! \57		-path "$NSIM1"/net -exec basename {} \;)58	DSTIF=$(find "$NSIM2"/net -maxdepth 1 -type d ! \59		-path "$NSIM2"/net -exec basename {} \;)60}61 62link_ifaces() {63	local NSIM_DEV_SYS_LINK="/sys/bus/netdevsim/link_device"64	local SRCIF_IFIDX=$(cat /sys/class/net/"$SRCIF"/ifindex)65	local DSTIF_IFIDX=$(cat /sys/class/net/"$DSTIF"/ifindex)66 67	exec {NAMESPACE_FD}</var/run/netns/"${NAMESPACE}"68	exec {INITNS_FD}</proc/self/ns/net69 70	# Bind the dst interface to namespace71	ip link set "${DSTIF}" netns "${NAMESPACE}"72 73	# Linking one device to the other one (on the other namespace}74	if ! echo "${INITNS_FD}:$SRCIF_IFIDX $NAMESPACE_FD:$DSTIF_IFIDX"  > $NSIM_DEV_SYS_LINK75	then76		echo "linking netdevsim1 with netdevsim2 should succeed"77		cleanup78		exit "${ksft_skip}"79	fi80}81 82function configure_ip() {83	# Configure the IPs for both interfaces84	ip netns exec "${NAMESPACE}" ip addr add "${DSTIP}"/24 dev "${DSTIF}"85	ip netns exec "${NAMESPACE}" ip link set "${DSTIF}" up86 87	ip addr add "${SRCIP}"/24 dev "${SRCIF}"88	ip link set "${SRCIF}" up89}90 91function set_network() {92	# setup_ns function is coming from lib.sh93	setup_ns NAMESPACE94 95	# Create both interfaces, and assign the destination to a different96	# namespace97	create_ifaces98 99	# Link both interfaces back to back100	link_ifaces101 102	configure_ip103}104 105function create_dynamic_target() {106	DSTMAC=$(ip netns exec "${NAMESPACE}" \107		 ip link show "${DSTIF}" | awk '/ether/ {print $2}')108 109	# Create a dynamic target110	mkdir "${NETCONS_PATH}"111 112	echo "${DSTIP}" > "${NETCONS_PATH}"/remote_ip113	echo "${SRCIP}" > "${NETCONS_PATH}"/local_ip114	echo "${DSTMAC}" > "${NETCONS_PATH}"/remote_mac115	echo "${SRCIF}" > "${NETCONS_PATH}"/dev_name116 117	echo 1 > "${NETCONS_PATH}"/enabled118}119 120function cleanup() {121	local NSIM_DEV_SYS_DEL="/sys/bus/netdevsim/del_device"122 123	# delete netconsole dynamic reconfiguration124	echo 0 > "${NETCONS_PATH}"/enabled125	# Remove the configfs entry126	rmdir "${NETCONS_PATH}"127 128	# Delete netdevsim devices129	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL"130	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL"131 132	# this is coming from lib.sh133	cleanup_all_ns134 135	# Restoring printk configurations136	echo "${DEFAULT_PRINTK_VALUES}" > /proc/sys/kernel/printk137}138 139function listen_port_and_save_to() {140	local OUTPUT=${1}141	# Just wait for 2 seconds142	timeout 2 ip netns exec "${NAMESPACE}" \143		socat UDP-LISTEN:"${PORT}",fork "${OUTPUT}"144}145 146function validate_result() {147	local TMPFILENAME="$1"148 149	# Check if the file exists150	if [ ! -f "$TMPFILENAME" ]; then151		echo "FAIL: File was not generated." >&2152		exit "${ksft_fail}"153	fi154 155	if ! grep -q "${MSG}" "${TMPFILENAME}"; then156		echo "FAIL: ${MSG} not found in ${TMPFILENAME}" >&2157		cat "${TMPFILENAME}" >&2158		exit "${ksft_fail}"159	fi160 161	# Delete the file once it is validated, otherwise keep it162	# for debugging purposes163	rm "${TMPFILENAME}"164	exit "${ksft_pass}"165}166 167function check_for_dependencies() {168	if [ "$(id -u)" -ne 0 ]; then169		echo "This test must be run as root" >&2170		exit "${ksft_skip}"171	fi172 173	if ! which socat > /dev/null ; then174		echo "SKIP: socat(1) is not available" >&2175		exit "${ksft_skip}"176	fi177 178	if ! which ip > /dev/null ; then179		echo "SKIP: ip(1) is not available" >&2180		exit "${ksft_skip}"181	fi182 183	if ! which udevadm > /dev/null ; then184		echo "SKIP: udevadm(1) is not available" >&2185		exit "${ksft_skip}"186	fi187 188	if [ ! -d "${NETCONS_CONFIGFS}" ]; then189		echo "SKIP: directory ${NETCONS_CONFIGFS} does not exist. Check if NETCONSOLE_DYNAMIC is enabled" >&2190		exit "${ksft_skip}"191	fi192 193	if ip link show "${DSTIF}" 2> /dev/null; then194		echo "SKIP: interface ${DSTIF} exists in the system. Not overwriting it." >&2195		exit "${ksft_skip}"196	fi197 198	if ip addr list | grep -E "inet.*(${SRCIP}|${DSTIP})" 2> /dev/null; then199		echo "SKIP: IPs already in use. Skipping it" >&2200		exit "${ksft_skip}"201	fi202}203 204# ========== #205# Start here #206# ========== #207modprobe netdevsim 2> /dev/null || true208modprobe netconsole 2> /dev/null || true209 210# The content of kmsg will be save to the following file211OUTPUT_FILE="/tmp/${TARGET}"212 213# Check for basic system dependency and exit if not found214check_for_dependencies215# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)216echo "6 5" > /proc/sys/kernel/printk217# Remove the namespace, interfaces and netconsole target on exit218trap cleanup EXIT219# Create one namespace and two interfaces220set_network221# Create a dynamic target for netconsole222create_dynamic_target223# Listed for netconsole port inside the namespace and destination interface224listen_port_and_save_to "${OUTPUT_FILE}" &225# Wait for socat to start and listen to the port.226wait_local_port_listen "${NAMESPACE}" "${PORT}" udp227# Send the message228echo "${MSG}: ${TARGET}" > /dev/kmsg229# Wait until socat saves the file to disk230busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"231 232# Make sure the message was received in the dst part233# and exit234validate_result "${OUTPUT_FILE}"235