Path: blob/master/tools/testing/selftests/drivers/net/netcons_fragmented_msg.sh
26288 views
#!/usr/bin/env bash1# SPDX-License-Identifier: GPL-2.023# Test netconsole's message fragmentation functionality.4#5# When a message exceeds the maximum packet size, netconsole splits it into6# multiple fragments for transmission. This test verifies:7# - Correct fragmentation of large messages8# - Proper reassembly of fragments at the receiver9# - Preservation of userdata across fragments10# - Behavior with and without kernel release version appending11#12# Author: Breno Leitao <[email protected]>1314set -euo pipefail1516SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")1718source "${SCRIPTDIR}"/lib/sh/lib_netcons.sh1920modprobe netdevsim 2> /dev/null || true21modprobe netconsole 2> /dev/null || true2223# The content of kmsg will be save to the following file24OUTPUT_FILE="/tmp/${TARGET}"2526# set userdata to a long value. In this case, it is "1-2-3-4...50-"27USERDATA_VALUE=$(printf -- '%.2s-' {1..60})2829# Convert the header string in a regexp, so, we can remove30# the second header as well.31# A header looks like "13,468,514729715,-,ncfrag=0/1135;". If32# release is appended, you might find something like:L33# "6.13.0-04048-g4f561a87745a,13,468,514729715,-,ncfrag=0/1135;"34function header_to_regex() {35# header is everything before ;36local HEADER="${1}"37REGEX=$(echo "${HEADER}" | cut -d'=' -f1)38echo "${REGEX}=[0-9]*\/[0-9]*;"39}4041# We have two headers in the message. Remove both to get the full message,42# and extract the full message.43function extract_msg() {44local MSGFILE="${1}"45# Extract the header, which is the very first thing that arrives in the46# first list.47HEADER=$(sed -n '1p' "${MSGFILE}" | cut -d';' -f1)48HEADER_REGEX=$(header_to_regex "${HEADER}")4950# Remove the two headers from the received message51# This will return the message without any header, similarly to what52# was sent.53sed "s/""${HEADER_REGEX}""//g" "${MSGFILE}"54}5556# Validate the message, which has two messages glued together.57# unwrap them to make sure all the characters were transmitted.58# File will look like the following:59# 13,468,514729715,-,ncfrag=0/1135;<message>60# key=<part of key>-13,468,514729715,-,ncfrag=967/1135;<rest of the key>61function validate_fragmented_result() {62# Discard the netconsole headers, and assemble the full message63RCVMSG=$(extract_msg "${1}")6465# check for the main message66if ! echo "${RCVMSG}" | grep -q "${MSG}"; then67echo "Message body doesn't match." >&268echo "msg received=" "${RCVMSG}" >&269exit "${ksft_fail}"70fi7172# check userdata73if ! echo "${RCVMSG}" | grep -q "${USERDATA_VALUE}"; then74echo "message userdata doesn't match" >&275echo "msg received=" "${RCVMSG}" >&276exit "${ksft_fail}"77fi78# test passed. hooray79}8081# Check for basic system dependency and exit if not found82check_for_dependencies83# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5)84echo "6 5" > /proc/sys/kernel/printk85# Remove the namespace, interfaces and netconsole target on exit86trap cleanup EXIT87# Create one namespace and two interfaces88set_network89# Create a dynamic target for netconsole90create_dynamic_target91# Set userdata "key" with the "value" value92set_user_data939495# TEST 1: Send message and userdata. They will fragment96# =======97MSG=$(printf -- 'MSG%.3s=' {1..150})9899# Listen for netconsole port inside the namespace and destination interface100listen_port_and_save_to "${OUTPUT_FILE}" &101# Wait for socat to start and listen to the port.102wait_local_port_listen "${NAMESPACE}" "${PORT}" udp103# Send the message104echo "${MSG}: ${TARGET}" > /dev/kmsg105# Wait until socat saves the file to disk106busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"107# Check if the message was not corrupted108validate_fragmented_result "${OUTPUT_FILE}"109110# TEST 2: Test with smaller message, and without release appended111# =======112MSG=$(printf -- 'FOOBAR%.3s=' {1..100})113# Let's disable release and test again.114disable_release_append115116listen_port_and_save_to "${OUTPUT_FILE}" &117wait_local_port_listen "${NAMESPACE}" "${PORT}" udp118echo "${MSG}: ${TARGET}" > /dev/kmsg119busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}"120validate_fragmented_result "${OUTPUT_FILE}"121exit "${ksft_pass}"122123124