]> johannes.truschnigg.info Git - sqm_lagthrottle/commitdiff
Restore previously set bandwidth limits upon exit
authorJohannes Truschnigg <johannes@truschnigg.info>
Sat, 3 Sep 2022 17:14:44 +0000 (19:14 +0200)
committerJohannes Truschnigg <johannes@truschnigg.info>
Sat, 3 Sep 2022 17:14:44 +0000 (19:14 +0200)
sqm_lagthrottle.sh

index 4ee6558d09853c7c83038b7e1496edd361379a40..2c756e7dc051c74b52112fabb5943ee9e12612eb 100755 (executable)
@@ -222,6 +222,48 @@ case "${TREND_DECISION}" in
 esac
 
 
+saved_bw_rx="$(tc qdisc show dev "${IFACE_RX}" | awk '{if($2 == "cake" && $7 == "bandwidth"){print $8; exit}} END{print "undef"}' | head -n1)"
+case "${saved_bw_rx}" in
+  [0-9]*)
+    :
+  ;;
+  undef)
+    printf 'FATAL: Could not determine current bw cap for cake SQM on "%s"\n' "${IFACE_RX}" >&2
+    exit 1
+  ;;
+  *)
+    printf 'FATAL: Unexpected value retrieved from `tc` for cake SQM RX bandwidth on "%s"\n' "${IFACE_RX}" >&2
+    exit 1
+  ;;
+esac
+
+
+saved_bw_tx="$(tc qdisc show dev "${IFACE_TX}" | awk '{if($2 == "cake" && $7 == "bandwidth"){print $8; exit}} END{print "undef"}' | head -n1)"
+case "${saved_bw_tx}" in
+  [0-9]*)
+    :
+  ;;
+  undef)
+    printf 'FATAL: Could not determine current bw cap for cake SQM on "%s"\n' "${IFACE_TX}" >&2
+    exit 1
+  ;;
+  *)
+    printf 'FATAL: Unexpected value retrieved from `tc` for cake SQM TX bandwidth on "%s"\n' "${IFACE_TX}" >&2
+    exit 1
+  ;;
+esac
+
+
+_restore_saved_bw() {
+  echo 'Restoring saved bandwith limits...' >&2
+  (
+    set -x
+    tc qdisc change root dev "${IFACE_RX}" cake bandwidth "${saved_bw_rx}"
+    tc qdisc change root dev "${IFACE_TX}" cake bandwidth "${saved_bw_tx}"
+  )
+}
+
+
 set -u
 
 trap ':' SIGINT SIGTERM
@@ -242,4 +284,6 @@ fping --loop --retry=1 --size=1280 --period="${PING_INTERVAL}" --random ${PING_P
   -v PING_PEERS="${PING_PEERS}" \
   -f ./__sqm_lagthrottle.awk
 
-echo 'exiting - cleanup...' >&2
+echo 'Control loop exited - performing cleanup...' >&2
+_restore_saved_bw
+echo 'sqm_lagthrottle.sh has terminated.' >&2