1
0
Fork 0
mirror of https://github.com/Ysurac/openmptcprouter-feeds.git synced 2025-02-14 19:41:51 +00:00

Fix & update sqm autorate

This commit is contained in:
Ycarus (Yannick Chabanois) 2023-06-01 19:54:32 +02:00
parent fb63e3779a
commit 06e55401aa
7 changed files with 1111 additions and 884 deletions

View file

@ -8,7 +8,7 @@ LUCI_DESCRIPTION:=Luci interface for the SQM scripts queue management package wi
PKG_MAINTAINER:=Toke Høiland-Jørgensen <toke@toke.dk>
LUCI_DEPENDS:=+sqm-scripts +bash
LUCI_DEPENDS:=+sqm-scripts +bash +tsping
LUCI_PKGARCH:=all
include $(TOPDIR)/feeds/luci/luci.mk

View file

@ -29,8 +29,9 @@ _config_autorate() {
config_get upload "$1" upload
config_get max_upload "$1" max_upload
[ "${min_upload}" == "0" ] || [ "${max_upload}" == "0" ] || [ "${upload}" == "0" ] && return
config_get interface "$1" interface
cp /usr/share/sqm-autorate/cake-autorate_template.sh /usr/share/sqm-autorate/cake-autorate_config.${interface}.sh
# config_get interface "$1" interface
# cp /usr/share/sqm-autorate/cake-autorate_template.sh /usr/share/sqm-autorate/cake-autorate_config.${interface}.sh
cp /usr/share/sqm-autorate/cake-autorate_template.sh /usr/share/sqm-autorate/cake-autorate_config.$1.sh
}
_launch_autorate() {

View file

@ -7,8 +7,6 @@
# Author: @Lynx (OpenWrt forum)
# Inspiration taken from: @moeller0 (OpenWrt forum)
cake_autorate_version="2.0.0"
# *** OUTPUT AND LOGGING OPTIONS ***
output_processing_stats=1 # enable (1) or disable (0) output monitoring lines showing processing stats
@ -44,7 +42,7 @@ ul_if=wan # upload interface
# fping - round robin pinging (rtts)
# ping - (iputils-ping) individual pinging (rtts)
# hping3 - individidual pinging (owds)
pinger_binary=ping
pinger_binary=fping
# list of reflectors to use and number of pingers to initiate
# pingers will be initiated with reflectors in the order specified in the list
@ -187,9 +185,9 @@ reflector_misbehaving_detection_thr=3
reflector_replacement_interval_mins=60 # how often to replace a random reflector from the present list
reflector_comparison_interval_mins=1 # how often to compare reflectors
reflector_owd_baseline_delta_thr_ms=10 # max increase from min baseline before reflector rotated
reflector_owd_delta_ewma_delta_thr_ms=10 # mac increase from min delta ewma before reflector rotated
reflector_comparison_interval_mins=1 # how often to compare reflectors
reflector_sum_owd_baselines_delta_thr_ms=20 # max increase from min sum owd baselines before reflector rotated
reflector_owd_delta_ewma_delta_thr_ms=10 # max increase from min delta ewma before reflector rotated
# stall is detected when the following two conditions are met:
# 1) no reflector responses within $stall_detection_thr*$ping_response_interval_us; and

View file

@ -24,17 +24,22 @@ sleep_s()
# - https://github.com/lynxthecat/cake-autorate/issues/174#issuecomment-1460057382
# - https://github.com/lynxthecat/cake-autorate/issues/174#issuecomment-1460074498
log_msg "DEBUG" "sleep_s"
local sleep_duration_s=${1} # (seconds, e.g. 0.5, 1 or 1.5)
read -r -t "${sleep_duration_s}" -u "${__sleep_fd}" || : &
wait "${!}"
log_msg "DEBUG" "sleep_s read ${sleep_duration_s} - ${__sleep_fd}"
# read -r -t "${sleep_duration_s}" -u "${__sleep_fd}" || :
read -r -t "${sleep_duration_s}" -u "${__sleep_fd}" || true
log_msg "DEBUG" "sleep_s read done"
}
sleep_us()
{
log_msg "DEBUG" "WTF ?"
local sleep_duration_us=${1} # (microseconds)
log_msg "DEBUG" "sleep_us"
sleep_duration_s=000000${sleep_duration_us}
sleep_duration_s=$((10#${sleep_duration_s::-6})).${sleep_duration_s: -6}
log_msg "DEBUG" "go to sleep_s"
sleep_s "${sleep_duration_s}"
}
@ -53,6 +58,19 @@ sleep_remaining_tick_time()
fi
}
get_remaining_tick_time()
{
# updates sleep_duration_s remaining to end of tick duration
local t_start_us=${1} # (microseconds)
local tick_duration_us=${2} # (microseconds)
sleep_duration_us=$(( t_start_us + tick_duration_us - ${EPOCHREALTIME/./} ))
((sleep_duration_us<0)) && sleep_duration_us=0
sleep_duration_s=000000${sleep_duration_us}
sleep_duration_s=$((10#${sleep_duration_s::-6})).${sleep_duration_s: -6}
}
randomize_array()
{
local -n array=${1}
@ -85,171 +103,32 @@ unlock()
rm -f "${path:?}"
}
_proc_man_set_key()
terminate()
{
local key=${1}
local value=${2}
# Send regular kill to processes and monitor terminations;
# return as soon as all of the active processes terminate;
# if any processes remain active after one second, kill with fire using kill -9;
# and, finally, call wait on all processes to reap any zombie processes.
lock "${PROC_STATE_FILE_LOCK:?}"
trap 'unlock "${PROC_STATE_FILE_LOCK:?}"' RETURN
local pids=("${@:-}")
kill "${pids[@]}" 2> /dev/null
local entered=0
while read -r line; do
if [[ ${line} =~ ^${key}= ]]; then
printf '%s\n' "${key}=${value}"
entered=1
else
printf '%s\n' "${line}"
fi
done < "${PROC_STATE_FILE:?}" > "${PROC_STATE_FILE:?}.tmp"
if (( entered == 0 )); then
printf '%s\n' "${key}=${value}" >> "${PROC_STATE_FILE:?}.tmp"
fi
mv "${PROC_STATE_FILE:?}.tmp" "${PROC_STATE_FILE:?}"
return 0
for((i=0; i<10; i++))
do
for process in "${!pids[@]}"
do
kill -0 "${pids[${process}]}" 2> /dev/null || unset "pids[${process}]"
done
[[ "${pids[*]}" ]] || return
sleep_s 0.1
done
kill -9 "${pids[@]}" 2> /dev/null
wait "${@:-}"
}
_proc_man_get_key_value()
{
local key=${1}
lock "${PROC_STATE_FILE_LOCK:?}"
trap 'unlock "${PROC_STATE_FILE_LOCK:?}"' RETURN
while read -r line; do
if [[ ${line} =~ ^${key}= ]]; then
printf '%s\n' "${line#*=}"
return 0
fi
done < "${PROC_STATE_FILE:?}"
return 1
}
proc_man()
{
local action=${1}
local name=${2}
shift 2
if [[ ! -f "${PROC_STATE_FILE:?}" ]]; then
return 1
fi
# shellcheck disable=SC2311
case "${action}" in
"start")
pid=$(_proc_man_get_key_value "${name}")
if (( pid && pid > 0 )) && kill -0 "${pid}" 2> /dev/null; then
return 1;
fi
"${@}" &
local pid=${!}
_proc_man_set_key "${name}" "${pid}"
;;
"stop")
local pid
pid=$(_proc_man_get_key_value "${name}")
if ! (( pid && pid > 0 )); then
return 0;
fi
kill "${pid}" 2> /dev/null || true
# wait for process to die
killed=0
for ((i=0; i<10; i++));
do
if kill -0 "${pid}" 2> /dev/null; then
sleep_us 100000
else
killed=1
break
fi
done
# if process still alive, kill it with fire
if (( killed == 0 )); then
kill -9 "${pid}" 2> /dev/null || true
fi
_proc_man_set_key "${name}" "-1" "${PROC_STATE_FILE:?}"
;;
"status")
local pid
pid=$(_proc_man_get_key_value "${name}")
if (( pid && pid > 0 )); then
if kill -0 "${pid}" 2> /dev/null; then
printf '%s\n' "running"
else
printf '%s\n' "dead"
fi
else
printf '%s\n' "stopped"
fi
;;
"wait")
local pid
pid=$(_proc_man_get_key_value "${name}")
if (( pid && pid > 0 )); then
wait "${pid}" && return 0
fi
return 1
;;
"signal")
shift 3
local pid
pid=$(_proc_man_get_key_value "${name}")
if (( pid && pid > 0 )); then
kill -s "${1}" "${pid}" 2>/dev/null && return 0
fi
return 1
;;
"initialize")
proc_man_initialize
return $?
;;
*)
printf '%s\n' "unknown action: ${action}" >&2
return 1
;;
esac
return 0
}
proc_man_initialize()
{
true > "${PROC_STATE_FILE:?}"
}
proc_man_start()
{
proc_man start "${@}"
}
proc_man_stop()
{
proc_man stop "${@}"
}
proc_man_status()
{
proc_man status "${@}"
}
proc_man_wait()
{
proc_man wait "${@}"
}
proc_man_signal()
{
proc_man signal "${@}"
}
if (( __set_e == 1 )); then
set +e

View file

@ -7,17 +7,21 @@
# Author: @Lynx (OpenWrt forum)
# Inspiration taken from: @moeller0 (OpenWrt forum)
INTERFACE=$(basename "$0" | cut -d. -f2)
INTERFACE=$(basename "$1" | cut -d. -f2)
cake_autorate_version="2.0.0"
# *** OUTPUT AND LOGGING OPTIONS ***
output_processing_stats=$(uci -q get sqm.${INTERFACE}.output_processing_stats || echo '0') # enable (1) or disable (0) output monitoring lines showing processing stats
output_load_stats=$(uci -q get sqm.${INTERFACE}.output_load_stats || echo '0') # enable (1) or disable (0) output monitoring lines showing achieved loads
output_processing_stats=1
#$(uci -q get sqm.${INTERFACE}.output_processing_stats || echo '0') # enable (1) or disable (0) output monitoring lines showing processing stats
output_load_stats=0
#$(uci -q get sqm.${INTERFACE}.output_load_stats || echo '0') # enable (1) or disable (0) output monitoring lines showing achieved loads
output_reflector_stats=$(uci -q get sqm.${INTERFACE}.output_reflector_stats || echo '0') # enable (1) or disable (0) output monitoring lines showing reflector stats
output_cake_changes=$(uci -q get sqm.${INTERFACE}.output_cake_changes || echo '0') # enable (1) or disable (0) output monitoring lines showing cake bandwidth changes
debug=$(uci -q get sqm.${INTERFACE}.debug || echo '0') # enable (1) or disable (0) out of debug lines
output_cake_changes=0
#$(uci -q get sqm.${INTERFACE}.output_cake_changes || echo '0') # enable (1) or disable (0) output monitoring lines showing cake bandwidth changes
debug=0
#$(uci -q get sqm.${INTERFACE}.debug || echo '0') # enable (1) or disable (0) out of debug lines
# This can generate a LOT of records so be careful:
log_DEBUG_messages_to_syslog=0 # enable (1) or disable (0) logging of all DEBUG records into the system log.
@ -26,7 +30,7 @@ log_DEBUG_messages_to_syslog=0 # enable (1) or disable (0) logging of all DEBUG
# every write the cumulative write time and bytes associated with each log line are checked
# and if either exceeds the configured values below, the log log file is rotated
log_to_file=1 # enable (1) or disable (0) output logging to file (/tmp/cake-autorate.log)
log_file_max_time_mins=10 # maximum time between log file rotations
log_file_max_time_mins=10000 # maximum time between log file rotations
log_file_max_size_KB=2000 # maximum KB (i.e. bytes/1024) worth of log lines between log file rotations
# log file path defaults to /var/log/
@ -46,7 +50,7 @@ ul_if=$(uci -q get sqm.${INTERFACE}.interface) # upload interface
# fping - round robin pinging (rtts)
# ping - (iputils-ping) individual pinging (rtts)
# hping3 - individidual pinging (owds)
pinger_binary=ping
pinger_binary=tsping
# list of reflectors to use and number of pingers to initiate
# pingers will be initiated with reflectors in the order specified in the list
@ -54,16 +58,16 @@ pinger_binary=ping
# so e.g. if 6 reflectors are specified and the number of pingers is set to 4, the first 4 reflectors will be used initially
# and the remaining 2 reflectors in the list will be used in the event any of the first 4 go bad
# a bad reflector will go to the back of the queue on reflector rotation
reflectors=($(uci -q get omr-tracker.defaults.hosts))
#reflectors=(
#"1.1.1.1" "1.0.0.1" # Cloudflare
#"8.8.8.8" "8.8.4.4" # Google
#"9.9.9.9" "9.9.9.10" "9.9.9.11" # Quad9
#"94.140.14.15" "94.140.14.140" "94.140.14.141" "94.140.15.15" "94.140.15.16" # AdGuard
#"64.6.65.6" "156.154.70.1" "156.154.70.2" "156.154.70.3" "156.154.70.4" "156.154.70.5" "156.154.71.1" "156.154.71.2" "156.154.71.3" "156.154.71.4" "156.154.71.5" # Neustar
#"208.67.220.2" "208.67.220.123" "208.67.220.220" "208.67.222.2" "208.67.222.123" # OpenDNS
#"185.228.168.9" "185.228.168.10" # CleanBrowsing
#)
#reflectors=$(uci -q get omr-tracker.defaults.hosts)
reflectors=(
"1.1.1.1" "1.0.0.1" # Cloudflare
"8.8.8.8" "8.8.4.4" # Google
"9.9.9.9" "9.9.9.10" "9.9.9.11" # Quad9
"94.140.14.15" "94.140.14.140" "94.140.14.141" "94.140.15.15" "94.140.15.16" # AdGuard
"64.6.65.6" "156.154.70.1" "156.154.70.2" "156.154.70.3" "156.154.70.4" "156.154.70.5" "156.154.71.1" "156.154.71.2" "156.154.71.3" "156.154.71.4" "156.154.71.5" # Neustar
"208.67.220.2" "208.67.220.123" "208.67.220.220" "208.67.222.2" "208.67.222.123" # OpenDNS
"185.228.168.9" "185.228.168.10" # CleanBrowsing
)
randomize_reflectors=1 # enable (1) or disable (0) randomization of reflectors on startup
@ -71,14 +75,14 @@ randomize_reflectors=1 # enable (1) or disable (0) randomization of reflectors o
# to avoid excessive CPU use (proportional with ping interval / number of pingers)
# and to avoid abusive network activity (excessive ICMP frequency to one reflector)
# The author has found an ICMP rate of 1/(0.2/4) = 20 Hz to give satisfactory performance on 4G
no_pingers=$(uci -q get sqm.${INTERFACE}.no_pingers || echo "6") # number of pingers to maintain
reflector_ping_interval_s=$(uci -q get sqm.${INTERFACE}.reflector_ping_interval_s || echo "0.3") # (seconds, e.g. 0.2s or 2s)
no_pingers=$(uci -q get sqm.${INTERFACE}.no_pingers || echo "4") # number of pingers to maintain
reflector_ping_interval_s=$(uci -q get sqm.${INTERFACE}.reflector_ping_interval_s || echo "1") # (seconds, e.g. 0.2s or 2s)
# delay threshold in ms is the extent of OWD increase to classify as a delay
# these are automatically adjusted based on maximum on the wire packet size
# (adjustment significant at sub 12Mbit/s rates, else negligible)
dl_delay_thr_ms=$(uci -q get sqm.${INTERFACE}.delay_thr_ms || echo "30") # (milliseconds)
ul_delay_thr_ms=$(uci -q get sqm.${INTERFACE}.delay_thr_ms || echo "30") # (milliseconds)
dl_delay_thr_ms=$(uci -q get sqm.${INTERFACE}.delay_thr_ms || echo "100") # (milliseconds)
ul_delay_thr_ms=$(uci -q get sqm.${INTERFACE}.delay_thr_ms || echo "100") # (milliseconds)
# Set either of the below to 0 to adjust one direction only
# or alternatively set both to 0 to simply use cake-autorate to monitor a connection
@ -97,11 +101,11 @@ max_ul_shaper_rate_kbps=$(uci -q get sqm.${INTERFACE}.max_upload || echo "35000"
# pausing all active pingers when connection is not in active use
enable_sleep_function=$(uci -q get sqm.${INTERFACE}.enable_sleep_functions || echo "1") # enable (1) or disable (0) sleep functonality
connection_active_thr_kbps=$(uci -q get sqm.${INTERFACE}.connection_active_thr_kpbs || echo "1000") # threshold in Kbit/s below which dl/ul is considered idle
sustained_idle_sleep_thr_s=$(uci -q get sqm.${INTERFACE}.sustained_idle_sleep_thr || echo "60.0") # time threshold to put pingers to sleep on sustained dl/ul achieved rate < idle_thr (seconds)
sustained_idle_sleep_thr_s=$(uci -q get sqm.${INTERFACE}.sustained_idle_sleep_thr || echo "150.0") # time threshold to put pingers to sleep on sustained dl/ul achieved rate < idle_thr (seconds)
min_shaper_rates_enforcement=$(uci -q get sqm.${INTERFACE}.min_shaper_rates_enforcement || echo "0") # enable (1) or disable (0) dropping down to minimum shaper rates on connection idle or stall
startup_wait_s=$(uci -q get sqm.${INTERFACE}.startup_wait_s || echo "60.0") # number of seconds to wait on startup (e.g. to wait for things to settle on router reboot)
startup_wait_s=$(uci -q get sqm.${INTERFACE}.startup_wait_s || echo "0.0") # number of seconds to wait on startup (e.g. to wait for things to settle on router reboot)
# *** ADVANCED CONFIGURATION OPTIONS ***
@ -126,7 +130,8 @@ log_file_export_compress=1 # compress log file exports using gzip and append .gz
# Unfortunately, fping does not offer a command line switch to set
# the firewall mark.
# WARNING: no error checking so use at own risk!
ping_extra_args="- B -I ${INTERFACE}"
#ping_extra_args="-B -I ${INTERFACE}"
ping_extra_args=""
# a wrapper for ping binary - used as a prefix for the real command
# e.g., when using mwan3, it is recommended to set it like this:
@ -172,13 +177,13 @@ high_load_thr=0.75 # % of currently set bandwidth for detecting high load
# the bufferbloat refractory period should be greater than the
# average time it would take to replace the bufferbloat
# detection window with new samples upon a bufferbloat event
bufferbloat_refractory_period_ms=300 # (milliseconds)
bufferbloat_refractory_period_ms=2000 # (milliseconds)
decay_refractory_period_ms=1000 # (milliseconds)
# interval for checking reflector health
reflector_health_check_interval_s=1.0 # (seconds)
# deadline for reflector response not to be classified as an offence against reflector
reflector_response_deadline_s=1.0 # (seconds)
reflector_response_deadline_s=2.0 # (seconds)
# reflector misbehaving is detected when $reflector_misbehaving_detection_thr samples
# out of the last (reflector misbehaving detection window) samples are offences
@ -190,7 +195,7 @@ reflector_misbehaving_detection_thr=3
reflector_replacement_interval_mins=60 # how often to replace a random reflector from the present list
reflector_comparison_interval_mins=1 # how often to compare reflectors
reflector_owd_baseline_delta_thr_ms=10 # max increase from min baseline before reflector rotated
reflector_sum_owd_baseline_delta_thr_ms=30 # max increase from min sum owd baselines before reflector rotated
reflector_owd_delta_ewma_delta_thr_ms=10 # mac increase from min delta ewma before reflector rotated
# stall is detected when the following two conditions are met:
@ -199,7 +204,7 @@ reflector_owd_delta_ewma_delta_thr_ms=10 # mac increase from min delta ewma be
stall_detection_thr=5
connection_stall_thr_kbps=10
global_ping_response_timeout_s=10.0 # timeout to set shaper rates to min on no ping response whatsoever (seconds)
global_ping_response_timeout_s=100.0 # timeout to set shaper rates to min on no ping response whatsoever (seconds)
if_up_check_interval_s=10.0 # time to wait before re-checking if rx/tx bytes files exist (e.g. from boot state or sleep recovery)

38
tsping/Makefile Normal file
View file

@ -0,0 +1,38 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=tsping
PKG_VERSION:=0.2.3
PKG_RELEASE:=1
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/Lochnair/tsping
PKG_SOURCE_VERSION:=2560d79c95154d2e2c9aa6725135f6fc5cf091d7
PKG_MAINTAINER:=Nils Andreas Svee <contact@lochnair.net>
PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE.md
PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=1
PKG_BUILD_DEPENDS:=!USE_GLIBC:argp-standalone
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/meson.mk
define Package/tsping
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Utility for sending timestamp pings
URL:=https://github.com/Lochnair/tsping
endef
define Package/tsping/description
A simple application to send ICMP echo/timestamp requests
endef
define Package/tsping/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(MESON_BUILD_DIR)/tsping $(1)/usr/sbin/
endef
$(eval $(call BuildPackage,tsping))