Skip to content

Instantly share code, notes, and snippets.

@Strykar
Last active January 23, 2023 02:23
Show Gist options
  • Save Strykar/3fee0d1004bc67d8acd9d2c351b983b0 to your computer and use it in GitHub Desktop.
Save Strykar/3fee0d1004bc67d8acd9d2c351b983b0 to your computer and use it in GitHub Desktop.
Update SQM limits in openwrt 4 times a day
#!/bin/sh
# v0.1 Avinash H. Duduskar <[email protected]>
# This adjusts OpenWRT SQM settings by re-calculating up/down bandwidth
# Needs the Speedtest.net CLI binary - https://www.speedtest.net/apps/cli
# Call this script every 4 or 6 hours via cron
set -euf -o pipefail
_sqm_int="queue" # Your actual wan interface: ifstatus wan | grep -e l3_device
_pul="90" # Percentage of wan upload speed to set, 90-95 is good
_pdl="95" # Percentage of wan download speed to set, 90-95 is good
# User configurable variables end #
# Sanity check if current SQM settings exist
/sbin/uci get sqm.@"$_sqm_int"[0].upload >/dev/null 2>&1 \
|| { printf 'Unable to get current UCI SQM upload settings, \
try "uci sqm show" and check the interface set. Aborting.\n' >&2; exit 1; }
/sbin/uci get sqm.@"$_sqm_int"[0].download >/dev/null 2>&1 \
|| { printf 'Unable to get current UCI SQM download settings, \
try "uci sqm show" and check the interface set. Aborting.\n' >&2; exit 1; }
# Get our current SQM up/down speeds from UCI
_cul=$(/sbin/uci get sqm.@"$_sqm_int"[0].upload)
_cdl=$(/sbin/uci get sqm.@"$_sqm_int"[0].download)
# Get current wan up/down speed, turn off SQM first to get full bandwidth
/etc/init.d/sqm stop
_ul1=$(/root/speedtest --unit=B/s --progress=no --format=json \
| /usr/bin/jsonfilter -e '@.upload.bandwidth')
_dl1=$(/root/speedtest --unit=B/s --progress=no --format=json \
| /usr/bin/jsonfilter -e '@.download.bandwidth')
# Speedtest --units is broken, reports only bytes/sec, convert to Kbit/sec for SQM
_ul=$(/usr/bin/awk -v a="$_ul1" 'BEGIN {
OFMT = "%.0f"
print (a / 125) }')
_dl=$(/usr/bin/awk -v b="$_dl1" 'BEGIN {
OFMT = "%.0f"
print (b / 125) }')
# Calculate 90-95% of up/down bandwidth. Gawk's faster, see https://bit.ly/3rCd4ha
_sqmul=$(/usr/bin/awk -v percentage="$_pul" -v x="$_ul" 'BEGIN {
OFMT = "%.0f"
print (percentage/100 * x) }')
_sqmdl=$(/usr/bin/awk -v percentage="$_pdl" -v y="$_dl" 'BEGIN {
OFMT = "%.0f"
print (percentage/100 * y) }')
# Sanity check if our calculated bandwidth is >= 75% of the old bandwidth set in SQM
if ((4*"$_sqmul" >= 3*"$_cul"));then
printf '>25%% difference between your current (""$_cul"") \
and my calculated (""$_sqmul"") SQM upload speeds. Aborting.\n'
exit 2
fi
if ((4*"$_sqmdl" >= 3*"$_cdl"));then
printf '>25%% difference between your current (""$_cdl"") \
and my calculated (""$_sqmdl"") SQM download speeds. Aborting.\n'
exit 2
fi
# Update SQM bandwith in UCI
/sbin/uci set sqm.@"$_sqm_int"[0].upload="$_sqmul"
/sbin/uci set sqm.@"$_sqm_int"[0].download="$_sqmdl"
/sbin/uci commit
/etc/init.d/sqm start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment