Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active February 14, 2024 22:41
Show Gist options
  • Save ThinGuy/30d33f7ed4a5faaf5a5c08184bce4c69 to your computer and use it in GitHub Desktop.
Save ThinGuy/30d33f7ed4a5faaf5a5c08184bce4c69 to your computer and use it in GitHub Desktop.
systemd-networkd script to disable TSO, GSO, and GRO on all NICs reporting up
#!/bin/bash
sudo install -o0 -g0 -m0755 /dev/null /etc/networkd-dispatcher/routable.d/99-disable-tso-gso-gro.sh
cat <<'EOF' |sudo tee /etc/networkd-dispatcher/routable.d/99-disable-tso-gso-gro.sh
#!/bin/bash
#
# This script will be placed in /etc/networkd-dispatcher/routable.d/
# and made exectuable
#
# This script disables the following features on all NICs except virtual:
# - tcp-segmentation-offload (TSO)
# - generic segmentation offload (GSO)
# - generic receive offload (GRO)
# Create array of NICs that report being up.
declare -ag NICS=($(ip -br l|awk '!/lo/&&/ UP /{print $1}'|sort -uV))
for N in ${NICS[@]};do
printf "\n\e[1mChanging Offload Settings for ${N}\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\e[0m\n"
[[ $(ethtool --show-offload ${N}|awk '/tcp-segmentation-offload/{print $2}') =~ on ]] && { printf "\\e[3G- TCP Segmentation Offload (TSO) is currently enabled for NIC ${N}. Disabling...\n";ethtool -K ${N} tso off; } || { printf "\e[3G- TCP Segmentation Offload (TSO) is already disabled for NIC ${N}. Skipping....\n";true; }
[[ $(ethtool --show-offload ${N}|awk '/generic-segmentation-offload/{print $2}') =~ on ]] && { printf "\e[3G- Generic Segmentation Offload (GSO) is currently enabled for NIC ${N}. Disabling...\n";ethtool -K ${N} gso off; } || { printf "\e[3G- Generic Segmentation Offload (GSO) is already disabled for NIC ${N}. Skipping....\n";true; }
[[ $(ethtool --show-offload ${N}|awk '/generic-receive-offload/{print $2}') =~ on ]] && { printf "\e[3G- Generic Receive Segmentation Offload (GRO) is currently enabled for NIC ${N}. Disabling...\n";ethtool -K ${N} gro off; } || { printf "\e[3G- Generic Receive Offload (GRO) is already disabled for NIC ${N}. Skipping....\n";true; }
printf "\n\e[5G\e[1mNew Offload Settings for ${N}\n\e[5G━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\e[0m\n"
ethtool --show-offload ${N}|awk '/generic-segmentation-offload|generic-receive-offload|tcp-segmentation-offload/{gsub(/generic-segmentation-offload/,"gso",$1);gsub(/generic-receive-offload/,"gro",$1);gsub(/tcp-segmentation-offload/,"tso",$1);print $1,$2}'|sed -r 's/^/ - /g';
done
exit 0
EOF
# Test with the following:
# sudo /etc/networkd-dispatcher/routable.d/99-disable-tso-gso-gro.sh
@ThinGuy
Copy link
Author

ThinGuy commented Feb 14, 2024

$ sudo /etc/networkd-dispatcher/routable.d/99-disable-tso-gso-gro.sh

Changing Offload Settings for br0
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  - TCP Segmentation Offload (TSO) is already disabled for br0. Skipping....
  - Generic Segmentation Offload (GSO) is already disabled for br0. Skipping....
  - Generic Receive Offload (GRO) is already disabled for br0. Skipping....

    New Offload Settings for br0
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
      - tso: off
      - gso: off
      - gro: off

Changing Offload Settings for enp1s0
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  - TCP Segmentation Offload (TSO) is already disabled for enp1s0. Skipping....
  - Generic Segmentation Offload (GSO) is already disabled for enp1s0. Skipping....
  - Generic Receive Offload (GRO) is already disabled for enp1s0. Skipping....

    New Offload Settings for enp1s0
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
      - tso: off
      - gso: off
      - gro: off

Changing Offload Settings for wlp2s0
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  - TCP Segmentation Offload (TSO) is already disabled for wlp2s0. Skipping....
  - Generic Segmentation Offload (GSO) is already disabled for wlp2s0. Skipping....
  - Generic Receive Offload (GRO) is already disabled for wlp2s0. Skipping....

    New Offload Settings for wlp2s0
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
      - tso: off
      - gso: off
      - gro: off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment