1 #!/usr/bin/env bash 1 #!/usr/bin/env bash 2 # SPDX-License-Identifier: GPL-2.0 2 # SPDX-License-Identifier: GPL-2.0 3 3 4 # This test creates two netdevsim virtual inte 4 # This test creates two netdevsim virtual interfaces, assigns one of them (the 5 # "destination interface") to a new namespace, 5 # "destination interface") to a new namespace, and assigns IP addresses to both 6 # interfaces. 6 # interfaces. 7 # 7 # 8 # It listens on the destination interface usin 8 # It listens on the destination interface using socat and configures a dynamic 9 # target on netconsole, pointing to the destin 9 # target on netconsole, pointing to the destination IP address. 10 # 10 # 11 # Finally, it checks whether the message was r 11 # Finally, it checks whether the message was received properly on the 12 # destination interface. Note that this test 12 # destination interface. Note that this test may pollute the kernel log buffer 13 # (dmesg) and relies on dynamic configuration 13 # (dmesg) and relies on dynamic configuration and namespaces being configured. 14 # 14 # 15 # Author: Breno Leitao <leitao@debian.org> 15 # Author: Breno Leitao <leitao@debian.org> 16 16 17 set -euo pipefail 17 set -euo pipefail 18 18 19 SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOU 19 SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")") 20 20 21 # Simple script to test dynamic targets in net 21 # Simple script to test dynamic targets in netconsole 22 SRCIF="" # to be populated later 22 SRCIF="" # to be populated later 23 SRCIP=192.168.1.1 23 SRCIP=192.168.1.1 24 DSTIF="" # to be populated later 24 DSTIF="" # to be populated later 25 DSTIP=192.168.1.2 25 DSTIP=192.168.1.2 26 26 27 PORT="6666" 27 PORT="6666" 28 MSG="netconsole selftest" 28 MSG="netconsole selftest" 29 TARGET=$(mktemp -u netcons_XXXXX) 29 TARGET=$(mktemp -u netcons_XXXXX) 30 DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/p 30 DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/printk) 31 NETCONS_CONFIGFS="/sys/kernel/config/netconsol 31 NETCONS_CONFIGFS="/sys/kernel/config/netconsole" 32 NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}" 32 NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}" 33 # NAMESPACE will be populated by setup_ns with 33 # NAMESPACE will be populated by setup_ns with a random value 34 NAMESPACE="" 34 NAMESPACE="" 35 35 36 # IDs for netdevsim 36 # IDs for netdevsim 37 NSIM_DEV_1_ID=$((256 + RANDOM % 256)) 37 NSIM_DEV_1_ID=$((256 + RANDOM % 256)) 38 NSIM_DEV_2_ID=$((512 + RANDOM % 256)) 38 NSIM_DEV_2_ID=$((512 + RANDOM % 256)) 39 39 40 # Used to create and delete namespaces 40 # Used to create and delete namespaces 41 source "${SCRIPTDIR}"/../../net/lib.sh 41 source "${SCRIPTDIR}"/../../net/lib.sh 42 source "${SCRIPTDIR}"/../../net/net_helper.sh 42 source "${SCRIPTDIR}"/../../net/net_helper.sh 43 43 44 # Create netdevsim interfaces 44 # Create netdevsim interfaces 45 create_ifaces() { 45 create_ifaces() { 46 local NSIM_DEV_SYS_NEW=/sys/bus/netdev 46 local NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device 47 47 48 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS 48 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW" 49 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS 49 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW" 50 udevadm settle 2> /dev/null || true 50 udevadm settle 2> /dev/null || true 51 51 52 local NSIM1=/sys/bus/netdevsim/devices 52 local NSIM1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_1_ID" 53 local NSIM2=/sys/bus/netdevsim/devices 53 local NSIM2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_DEV_2_ID" 54 54 55 # These are global variables 55 # These are global variables 56 SRCIF=$(find "$NSIM1"/net -maxdepth 1 56 SRCIF=$(find "$NSIM1"/net -maxdepth 1 -type d ! \ 57 -path "$NSIM1"/net -exec basen 57 -path "$NSIM1"/net -exec basename {} \;) 58 DSTIF=$(find "$NSIM2"/net -maxdepth 1 58 DSTIF=$(find "$NSIM2"/net -maxdepth 1 -type d ! \ 59 -path "$NSIM2"/net -exec basen 59 -path "$NSIM2"/net -exec basename {} \;) 60 } 60 } 61 61 62 link_ifaces() { 62 link_ifaces() { 63 local NSIM_DEV_SYS_LINK="/sys/bus/netd 63 local NSIM_DEV_SYS_LINK="/sys/bus/netdevsim/link_device" 64 local SRCIF_IFIDX=$(cat /sys/class/net 64 local SRCIF_IFIDX=$(cat /sys/class/net/"$SRCIF"/ifindex) 65 local DSTIF_IFIDX=$(cat /sys/class/net 65 local DSTIF_IFIDX=$(cat /sys/class/net/"$DSTIF"/ifindex) 66 66 67 exec {NAMESPACE_FD}</var/run/netns/"${ 67 exec {NAMESPACE_FD}</var/run/netns/"${NAMESPACE}" 68 exec {INITNS_FD}</proc/self/ns/net 68 exec {INITNS_FD}</proc/self/ns/net 69 69 70 # Bind the dst interface to namespace 70 # Bind the dst interface to namespace 71 ip link set "${DSTIF}" netns "${NAMESP 71 ip link set "${DSTIF}" netns "${NAMESPACE}" 72 72 73 # Linking one device to the other one 73 # Linking one device to the other one (on the other namespace} 74 if ! echo "${INITNS_FD}:$SRCIF_IFIDX $ 74 if ! echo "${INITNS_FD}:$SRCIF_IFIDX $NAMESPACE_FD:$DSTIF_IFIDX" > $NSIM_DEV_SYS_LINK 75 then 75 then 76 echo "linking netdevsim1 with 76 echo "linking netdevsim1 with netdevsim2 should succeed" 77 cleanup 77 cleanup 78 exit "${ksft_skip}" 78 exit "${ksft_skip}" 79 fi 79 fi 80 } 80 } 81 81 82 function configure_ip() { 82 function configure_ip() { 83 # Configure the IPs for both interface 83 # Configure the IPs for both interfaces 84 ip netns exec "${NAMESPACE}" ip addr a 84 ip netns exec "${NAMESPACE}" ip addr add "${DSTIP}"/24 dev "${DSTIF}" 85 ip netns exec "${NAMESPACE}" ip link s 85 ip netns exec "${NAMESPACE}" ip link set "${DSTIF}" up 86 86 87 ip addr add "${SRCIP}"/24 dev "${SRCIF 87 ip addr add "${SRCIP}"/24 dev "${SRCIF}" 88 ip link set "${SRCIF}" up 88 ip link set "${SRCIF}" up 89 } 89 } 90 90 91 function set_network() { 91 function set_network() { 92 # setup_ns function is coming from lib 92 # setup_ns function is coming from lib.sh 93 setup_ns NAMESPACE 93 setup_ns NAMESPACE 94 94 95 # Create both interfaces, and assign t 95 # Create both interfaces, and assign the destination to a different 96 # namespace 96 # namespace 97 create_ifaces 97 create_ifaces 98 98 99 # Link both interfaces back to back 99 # Link both interfaces back to back 100 link_ifaces 100 link_ifaces 101 101 102 configure_ip 102 configure_ip 103 } 103 } 104 104 105 function create_dynamic_target() { 105 function create_dynamic_target() { 106 DSTMAC=$(ip netns exec "${NAMESPACE}" 106 DSTMAC=$(ip netns exec "${NAMESPACE}" \ 107 ip link show "${DSTIF}" | awk 107 ip link show "${DSTIF}" | awk '/ether/ {print $2}') 108 108 109 # Create a dynamic target 109 # Create a dynamic target 110 mkdir "${NETCONS_PATH}" 110 mkdir "${NETCONS_PATH}" 111 111 112 echo "${DSTIP}" > "${NETCONS_PATH}"/re 112 echo "${DSTIP}" > "${NETCONS_PATH}"/remote_ip 113 echo "${SRCIP}" > "${NETCONS_PATH}"/lo 113 echo "${SRCIP}" > "${NETCONS_PATH}"/local_ip 114 echo "${DSTMAC}" > "${NETCONS_PATH}"/r 114 echo "${DSTMAC}" > "${NETCONS_PATH}"/remote_mac 115 echo "${SRCIF}" > "${NETCONS_PATH}"/de 115 echo "${SRCIF}" > "${NETCONS_PATH}"/dev_name 116 116 117 echo 1 > "${NETCONS_PATH}"/enabled 117 echo 1 > "${NETCONS_PATH}"/enabled 118 } 118 } 119 119 120 function cleanup() { 120 function cleanup() { 121 local NSIM_DEV_SYS_DEL="/sys/bus/netde 121 local NSIM_DEV_SYS_DEL="/sys/bus/netdevsim/del_device" 122 122 123 # delete netconsole dynamic reconfigur 123 # delete netconsole dynamic reconfiguration 124 echo 0 > "${NETCONS_PATH}"/enabled 124 echo 0 > "${NETCONS_PATH}"/enabled 125 # Remove the configfs entry 125 # Remove the configfs entry 126 rmdir "${NETCONS_PATH}" 126 rmdir "${NETCONS_PATH}" 127 127 128 # Delete netdevsim devices 128 # Delete netdevsim devices 129 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS 129 echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL" 130 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS 130 echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL" 131 131 132 # this is coming from lib.sh 132 # this is coming from lib.sh 133 cleanup_all_ns 133 cleanup_all_ns 134 134 135 # Restoring printk configurations 135 # Restoring printk configurations 136 echo "${DEFAULT_PRINTK_VALUES}" > /pro 136 echo "${DEFAULT_PRINTK_VALUES}" > /proc/sys/kernel/printk 137 } 137 } 138 138 139 function listen_port_and_save_to() { 139 function listen_port_and_save_to() { 140 local OUTPUT=${1} 140 local OUTPUT=${1} 141 # Just wait for 2 seconds 141 # Just wait for 2 seconds 142 timeout 2 ip netns exec "${NAMESPACE}" 142 timeout 2 ip netns exec "${NAMESPACE}" \ 143 socat UDP-LISTEN:"${PORT}",for 143 socat UDP-LISTEN:"${PORT}",fork "${OUTPUT}" 144 } 144 } 145 145 146 function validate_result() { 146 function validate_result() { 147 local TMPFILENAME="$1" 147 local TMPFILENAME="$1" 148 148 149 # Check if the file exists 149 # Check if the file exists 150 if [ ! -f "$TMPFILENAME" ]; then 150 if [ ! -f "$TMPFILENAME" ]; then 151 echo "FAIL: File was not gener 151 echo "FAIL: File was not generated." >&2 152 exit "${ksft_fail}" 152 exit "${ksft_fail}" 153 fi 153 fi 154 154 155 if ! grep -q "${MSG}" "${TMPFILENAME}" 155 if ! grep -q "${MSG}" "${TMPFILENAME}"; then 156 echo "FAIL: ${MSG} not found i 156 echo "FAIL: ${MSG} not found in ${TMPFILENAME}" >&2 157 cat "${TMPFILENAME}" >&2 157 cat "${TMPFILENAME}" >&2 158 exit "${ksft_fail}" 158 exit "${ksft_fail}" 159 fi 159 fi 160 160 161 # Delete the file once it is validated 161 # Delete the file once it is validated, otherwise keep it 162 # for debugging purposes 162 # for debugging purposes 163 rm "${TMPFILENAME}" 163 rm "${TMPFILENAME}" 164 exit "${ksft_pass}" 164 exit "${ksft_pass}" 165 } 165 } 166 166 167 function check_for_dependencies() { 167 function check_for_dependencies() { 168 if [ "$(id -u)" -ne 0 ]; then 168 if [ "$(id -u)" -ne 0 ]; then 169 echo "This test must be run as 169 echo "This test must be run as root" >&2 170 exit "${ksft_skip}" 170 exit "${ksft_skip}" 171 fi 171 fi 172 172 173 if ! which socat > /dev/null ; then 173 if ! which socat > /dev/null ; then 174 echo "SKIP: socat(1) is not av 174 echo "SKIP: socat(1) is not available" >&2 175 exit "${ksft_skip}" 175 exit "${ksft_skip}" 176 fi 176 fi 177 177 178 if ! which ip > /dev/null ; then 178 if ! which ip > /dev/null ; then 179 echo "SKIP: ip(1) is not avail 179 echo "SKIP: ip(1) is not available" >&2 180 exit "${ksft_skip}" 180 exit "${ksft_skip}" 181 fi 181 fi 182 182 183 if ! which udevadm > /dev/null ; then 183 if ! which udevadm > /dev/null ; then 184 echo "SKIP: udevadm(1) is not 184 echo "SKIP: udevadm(1) is not available" >&2 185 exit "${ksft_skip}" 185 exit "${ksft_skip}" 186 fi 186 fi 187 187 188 if [ ! -d "${NETCONS_CONFIGFS}" ]; the 188 if [ ! -d "${NETCONS_CONFIGFS}" ]; then 189 echo "SKIP: directory ${NETCON 189 echo "SKIP: directory ${NETCONS_CONFIGFS} does not exist. Check if NETCONSOLE_DYNAMIC is enabled" >&2 190 exit "${ksft_skip}" 190 exit "${ksft_skip}" 191 fi 191 fi 192 192 193 if ip link show "${DSTIF}" 2> /dev/nul 193 if ip link show "${DSTIF}" 2> /dev/null; then 194 echo "SKIP: interface ${DSTIF} 194 echo "SKIP: interface ${DSTIF} exists in the system. Not overwriting it." >&2 195 exit "${ksft_skip}" 195 exit "${ksft_skip}" 196 fi 196 fi 197 197 198 if ip addr list | grep -E "inet.*(${SR 198 if ip addr list | grep -E "inet.*(${SRCIP}|${DSTIP})" 2> /dev/null; then 199 echo "SKIP: IPs already in use 199 echo "SKIP: IPs already in use. Skipping it" >&2 200 exit "${ksft_skip}" 200 exit "${ksft_skip}" 201 fi 201 fi 202 } 202 } 203 203 204 # ========== # 204 # ========== # 205 # Start here # 205 # Start here # 206 # ========== # 206 # ========== # 207 modprobe netdevsim 2> /dev/null || true 207 modprobe netdevsim 2> /dev/null || true 208 modprobe netconsole 2> /dev/null || true 208 modprobe netconsole 2> /dev/null || true 209 209 210 # The content of kmsg will be save to the foll 210 # The content of kmsg will be save to the following file 211 OUTPUT_FILE="/tmp/${TARGET}" 211 OUTPUT_FILE="/tmp/${TARGET}" 212 212 213 # Check for basic system dependency and exit i 213 # Check for basic system dependency and exit if not found 214 check_for_dependencies 214 check_for_dependencies 215 # Set current loglevel to KERN_INFO(6), and de 215 # Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5) 216 echo "6 5" > /proc/sys/kernel/printk 216 echo "6 5" > /proc/sys/kernel/printk 217 # Remove the namespace, interfaces and netcons 217 # Remove the namespace, interfaces and netconsole target on exit 218 trap cleanup EXIT 218 trap cleanup EXIT 219 # Create one namespace and two interfaces 219 # Create one namespace and two interfaces 220 set_network 220 set_network 221 # Create a dynamic target for netconsole 221 # Create a dynamic target for netconsole 222 create_dynamic_target 222 create_dynamic_target 223 # Listed for netconsole port inside the namesp 223 # Listed for netconsole port inside the namespace and destination interface 224 listen_port_and_save_to "${OUTPUT_FILE}" & 224 listen_port_and_save_to "${OUTPUT_FILE}" & 225 # Wait for socat to start and listen to the po 225 # Wait for socat to start and listen to the port. 226 wait_local_port_listen "${NAMESPACE}" "${PORT} 226 wait_local_port_listen "${NAMESPACE}" "${PORT}" udp 227 # Send the message 227 # Send the message 228 echo "${MSG}: ${TARGET}" > /dev/kmsg 228 echo "${MSG}: ${TARGET}" > /dev/kmsg 229 # Wait until socat saves the file to disk 229 # Wait until socat saves the file to disk 230 busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTP 230 busywait "${BUSYWAIT_TIMEOUT}" test -s "${OUTPUT_FILE}" 231 231 232 # Make sure the message was received in the ds 232 # Make sure the message was received in the dst part 233 # and exit 233 # and exit 234 validate_result "${OUTPUT_FILE}" 234 validate_result "${OUTPUT_FILE}"
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.