Skip to content

Commit

Permalink
runners: Expose a 'cleanup' action for silent cleanup of an interface
Browse files Browse the repository at this point in the history
If a network interface is removed before SQM is stopped on that interface,
removing the qdisc from that interface will result in errors in the log.
Whereas not running the 'stop' action will result in leftover state
files (and possibly an IFB interface).

While the right thing to do would be to stop SQM before removing the
interface, this is not always possible; for instance, pppd doesn't have a
'pre-down' hook. So to accommodate this use case, add a new 'cleanup'
action, which just unwinds the firewall rules, removes the IFB
interface (but silently in case it does not exist), and deletes the state
file.

Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
  • Loading branch information
tohojo committed Aug 22, 2023
1 parent 30f2cee commit 33a89d8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
10 changes: 8 additions & 2 deletions platform/linux/sqm-bin
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
. /etc/sqm/sqm.conf
ACTION="$1"
RUN_IFACE="$2"
CLEANUP=0

if [ "$ACTION" = "cleanup" ]; then
CLEANUP=1
ACTION=stop
fi

if [ "$(id -u)" -ne "0" ]; then
echo "This script must be run as root." >&2
exit 1
fi

if [ "$ACTION" != "start" -a "$ACTION" != "stop" -a "$ACTION" != "reload" ]; then
echo "Usage: $0 <start|stop|reload> [iface]." >&2
echo "Usage: $0 <start|stop|reload|cleanup> [iface]." >&2
exit 1
fi

Expand All @@ -20,7 +26,7 @@ if [ "$ACTION" = "stop" -a -z "$RUN_IFACE" ]; then
for f in ${SQM_STATE_DIR}/*.state; do
# Source the state file prior to stopping; we need the $IFACE and
# $SCRIPT variables saved in there.
[ -f "$f" ] && ( . $f; IFACE=$IFACE SCRIPT=$SCRIPT SQM_DEBUG=$SQM_DEBUG SQM_DEBUG_LOG=$SQM_DEBUG_LOG OUTPUT_TARGET=$OUTPUT_TARGET ${SQM_LIB_DIR}/stop-sqm )
[ -f "$f" ] && ( . $f; IFACE=$IFACE SCRIPT=$SCRIPT CLEANUP=$CLEANUP SQM_DEBUG=$SQM_DEBUG SQM_DEBUG_LOG=$SQM_DEBUG_LOG OUTPUT_TARGET=$OUTPUT_TARGET ${SQM_LIB_DIR}/stop-sqm )
done
exit 0
fi
Expand Down
4 changes: 4 additions & 0 deletions src/defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ fi
# particular command
SILENT=0

# If set to 1, stop-sqm will run the (silent) cleanup function instead of a full
# stop operation
[ -z "$CLEANUP" ] && CLEANUP=0

# Transaction log for unwinding ipt rules
IPT_TRANS_LOG="${SQM_STATE_DIR}/${IFACE}.iptables.log"

Expand Down
8 changes: 7 additions & 1 deletion src/run-openwrt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ ACTION="${1:-start}"
RUN_IFACE="$2"
LOCKDIR="${SQM_STATE_DIR}/sqm-run.lock"

CLEANUP=0
if [ "$ACTION" = "cleanup" ]; then
CLEANUP=1
ACTION=stop
fi

check_state_dir
[ -d "${SQM_QDISC_STATE_DIR}" ] || ${SQM_LIB_DIR}/update-available-qdiscs

Expand All @@ -26,7 +32,7 @@ stop_statefile() {
# there.
[ -f "$f" ] && ( . "$f";
IFACE=$IFACE SCRIPT=$SCRIPT SQM_DEBUG=$SQM_DEBUG \
SQM_DEBUG_LOG=$SQM_DEBUG_LOG \
SQM_DEBUG_LOG=$SQM_DEBUG_LOG CLEANUP=$CLEANUP \
SQM_VERBOSITY_MAX=$SQM_VERBOSITY_MAX \
SQM_VERBOSITY_MIN=$SQM_VERBOSITY_MIN \
OUTPUT_TARGET=$OUTPUT_TARGET ${SQM_LIB_DIR}/stop-sqm )
Expand Down
9 changes: 7 additions & 2 deletions src/stop-sqm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ if [ -z "${SCRIPT}" ] ; then
fi

sqm_trace; sqm_trace "$(date): Stopping." # Add some space and a date stamp to verbose log output and log files to separate runs
sqm_log "Stopping SQM on ${IFACE}"

# make sure to only delete the ifb associated with the current interface
CUR_IFB=$( get_ifb_associated_with_if ${IFACE} )
Expand All @@ -48,7 +47,13 @@ fi

. "${SQM_LIB_DIR}/$SCRIPT"

sqm_stop
if [ "$CLEANUP" -eq "1" ]; then
sqm_log "Cleaning up SQM state on ${IFACE}"
sqm_cleanup 1
else
sqm_log "Stopping SQM on ${IFACE}"
sqm_stop
fi
rm -f "${STATE_FILE}"

exit 0

0 comments on commit 33a89d8

Please sign in to comment.