diff --git a/xCAT-test/bats/xcatlib_nic_mtu_changed.bats b/xCAT-test/bats/xcatlib_nic_mtu_changed.bats new file mode 100644 index 0000000000..75bae77e8e --- /dev/null +++ b/xCAT-test/bats/xcatlib_nic_mtu_changed.bats @@ -0,0 +1,61 @@ +#!/usr/bin/env bats +# +# xcat_nic_mtu_changed is the decision configeth makes when it has to choose between writing +# the configuration files and also restarting the NIC. Its address comparison cannot see an +# MTU change, so a run that changes nothing else left the NIC on its old MTU: the profile said +# 1496 and `ip link` still said 1500 (confignetwork_secondarynic_updatenode on EL8 and EL9). +# +# `ip` is stubbed, so nothing on the host is read. + +load 'helpers/shell_source' + +setup() +{ + LIB="$(require_repo_file 'xCAT/postscripts/xcatlib.sh')" + BIN="${BATS_TEST_TMPDIR}/bin" + mkdir -p "$BIN" + export PATH="$BIN:$PATH" +} + +# Write an `ip` that reports $1 as the line for `ip link show dev `. +stub_ip() +{ + printf '#!/bin/sh\nprintf "%%s\\n" "%s"\n' "$1" >"$BIN/ip" + chmod 0755 "$BIN/ip" +} + +stub_ip_absent_device() +{ + printf '#!/bin/sh\necho "Device does not exist" >&2\nexit 1\n' >"$BIN/ip" + chmod 0755 "$BIN/ip" +} + +@test "a wanted MTU different from the live one is a change" { + stub_ip "3: ens4: mtu 1500 qdisc fq_codel state UP group default qlen 1000" + . "$LIB" + run xcat_nic_mtu_changed ens4 1496 + [ "$status" -eq 0 ] +} + +@test "a wanted MTU equal to the live one is not a change" { + stub_ip "3: ens4: mtu 1496 qdisc fq_codel state UP group default qlen 1000" + . "$LIB" + run xcat_nic_mtu_changed ens4 1496 + [ "$status" -ne 0 ] +} + +@test "a network that declares no MTU is not a change" { + stub_ip "3: ens4: mtu 1500 qdisc fq_codel state UP group default qlen 1000" + . "$LIB" + run xcat_nic_mtu_changed ens4 "" + [ "$status" -ne 0 ] + run xcat_nic_mtu_changed ens4 default + [ "$status" -ne 0 ] +} + +@test "a NIC whose MTU cannot be read is not a change" { + stub_ip_absent_device + . "$LIB" + run xcat_nic_mtu_changed ens4 1496 + [ "$status" -ne 0 ] +} diff --git a/xCAT/postscripts/configeth b/xCAT/postscripts/configeth index c556e64a29..b06b467c0d 100755 --- a/xCAT/postscripts/configeth +++ b/xCAT/postscripts/configeth @@ -1564,7 +1564,20 @@ else bool_restart_flag=1 bool_modify_flag=1 fi - + + #The restart below is what hands a changed MTU to the running NIC. + if [ $bool_restart_flag -eq 0 ];then + for str_mtu_wanted in "${array_nic_mtu[@]}" + do + if xcat_nic_mtu_changed "$str_nic_name" "$str_mtu_wanted"; then + log_info "configeth on $NODE: $str_nic_name mtu changes to $str_mtu_wanted." + bool_restart_flag=1 + bool_modify_flag=1 + break + fi + done + fi + if [ $bool_restart_flag = 0 ];then #add the new defined ip old_ifs=$IFS diff --git a/xCAT/postscripts/xcatlib.sh b/xCAT/postscripts/xcatlib.sh index d9e3bed29e..31f7f9cc4e 100755 --- a/xCAT/postscripts/xcatlib.sh +++ b/xCAT/postscripts/xcatlib.sh @@ -61,6 +61,24 @@ function hashget(){ eval echo "\$${str_hashname}" } +# True when the live MTU of NIC $1 differs from the MTU $2 the networks table declares. +# A wanted value that is empty or not a number means the network declares no MTU. +xcat_nic_mtu_changed() +{ + local nic="$1" + local wanted="$2" + local live + + [ -n "$nic" ] || return 1 + case "$wanted" in + ''|*[!0-9]*) return 1 ;; + esac + + live=$(ip link show dev "$nic" 2>/dev/null | sed -n 's/.*[[:space:]]mtu[[:space:]]\{1,\}\([0-9]\{1,\}\).*/\1/p' | head -1) + [ -n "$live" ] || return 1 + [ "$live" != "$wanted" ] +} + # Match the OSVER forms currently used by EL9+ postscript paths. xcat_is_el9_or_later() {