Skip to content

Commit

Permalink
mwan3: improve startup performance; version 2.9.0
Browse files Browse the repository at this point in the history
improve startup and runtime performance by

1) moving common startup procedures out of hotplug script when called
from mwan3 start
2) reducing calls to iptables to check status of rules
3) consolidating iptables updates and updating with iptables-restore
4) do not wait for kill if nothing was killed
5) running interface hotplug scripts in parallel
6) eliminate operations in hotplug script that check status on every
single interface unnecessarily
7) consolidate how mwan3track makes hotplug calls
8) do not restart mwan3track on connected events

This is a significant refactor, but should not result in any breaking
changes or require users to update their configurations.

version bump to 2.9.0

Signed-off-by: Aaron Goodman <[email protected]>
  • Loading branch information
aaronjg authored and farmergreg committed Sep 8, 2020
1 parent c766914 commit 36fcb67
Show file tree
Hide file tree
Showing 9 changed files with 722 additions and 572 deletions.
4 changes: 2 additions & 2 deletions net/mwan3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=mwan3
PKG_VERSION:=2.8.12
PKG_RELEASE:=2
PKG_VERSION:=2.9.0
PKG_RELEASE:=1
PKG_MAINTAINER:=Florian Eckert <[email protected]>
PKG_LICENSE:=GPL-2.0

Expand Down
113 changes: 59 additions & 54 deletions net/mwan3/files/etc/hotplug.d/iface/15-mwan3
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,101 @@
. /lib/functions/network.sh
. /lib/mwan3/mwan3.sh
. /usr/share/libubox/jshn.sh
. /lib/mwan3/common.sh

[ "$ACTION" == "ifup" -o "$ACTION" == "ifdown" ] || exit 1
SCRIPTNAME="mwan3-hotplug"
[ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifdown" ] || [ "$ACTION" = "connected" ] || [ "$ACTION" = "disconnected" ] || exit 1
[ -n "$INTERFACE" ] || exit 2

if [ "$ACTION" == "ifup" ]; then
[ -n "$DEVICE" ] || exit 3
if ( [ "$ACTION" = "ifup" ] || [ "$ACTION" = "connected" ] ) && [ -z "$DEVICE" ]; then
LOG notice "$ACTION called on $INTERFACE with no device set"
exit 3
fi

mwan3_lock "$ACTION" "$INTERFACE"
[ "$MWAN3_STARTUP" = 1 ] || mwan3_lock "$ACTION" "$INTERFACE"

config_load mwan3
config_get_bool enabled globals 'enabled' '0'
[ "${enabled}" -gt 0 ] || {
mwan3_unlock "$ACTION" "$INTERFACE"
[ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE"
LOG notice "mwan3 hotplug on $INTERFACE not called because globally disabled"
mwan3_flush_conntrack "$INTERFACE" "$ACTION"
exit 0
}

$IPT4 -S mwan3_hook &>/dev/null || {
mwan3_unlock "$ACTION" "$INTERFACE"
LOG warn "hotplug called on $INTERFACE before mwan3 has been set up"
exit 0
}

mwan3_init
mwan3_set_connected_iptables
mwan3_set_custom_ipset
[ "$MWAN3_STARTUP" = 1 ] || {
mwan3_set_connected_iptables
mwan3_set_custom_ipset
}

if [ "$MWAN3_STARTUP" != 1 ]; then
mwan3_set_user_iface_rules $INTERFACE $DEVICE
fi

config_get initial_state $INTERFACE initial_state "online"
config_get_bool enabled $INTERFACE 'enabled' '0'
[ "${enabled}" -eq 1 ] || {
mwan3_unlock "$ACTION" "$INTERFACE"
[ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE"
LOG notice "mwan3 hotplug on $INTERFACE not called because interface disabled"
exit 0
}

if [ "$ACTION" = "ifup" ]; then
config_get family $INTERFACE family ipv4
if [ "$family" = "ipv4" ]; then
ubus call network.interface.${INTERFACE}_4 status &>/dev/null
if [ "$?" -eq "0" ]; then
network_get_ipaddr src_ip ${INTERFACE}_4
else
network_get_ipaddr src_ip ${INTERFACE}
fi
[ -n "$src_ip" ] || src_ip="0.0.0.0"
elif [ "$family" = "ipv6" ]; then
ubus call network.interface.${INTERFACE}_6 status &>/dev/null
if [ "$?" -eq "0" ]; then
network_get_ipaddr6 src_ip ${INTERFACE}_6
else
network_get_ipaddr6 src_ip ${INTERFACE}
fi
[ -n "$src_ip" ] || src_ip="::"
fi
fi
trackpid=$(pgrep -f "mwan3track $INTERFACE ")

if [ "$initial_state" = "offline" ]; then
json_load "$(ubus call mwan3 status '{"section":"interfaces"}')"
json_select "interfaces"
json_select "${INTERFACE}"
json_get_var running running
json_get_var status status
status=$(cat $MWAN3TRACK_STATUS_DIR/$INTERFACE/STATUS 2>/dev/null || echo unknown)
else
status=online
running=1
fi

$LOG notice "Execute "$ACTION" event on interface $INTERFACE (${DEVICE:-unknown})"
[ -z "$TRUE_INTERFACE" ] && mwan3_get_true_iface TRUE_INTERFACE $INTERFACE

binary_status=$status
[ "$binary_status" = "online" ] || binary_status=offline

LOG notice "Execute "$ACTION" event on interface $INTERFACE (${DEVICE:-unknown})"

case "$ACTION" in
ifup)
mwan3_set_general_rules
mwan3_set_general_iptables
ifup|connected)
mwan3_create_iface_iptables $INTERFACE $DEVICE
mwan3_create_iface_rules $INTERFACE $DEVICE
mwan3_create_iface_route $INTERFACE $DEVICE
if [ "${running}" -eq 1 ] && [ "${status}" = "online" ]; then
$LOG notice "Starting tracker on interface $INTERFACE (${DEVICE:-unknown})"
mwan3_set_iface_hotplug_state $INTERFACE "online"
mwan3_track $INTERFACE $DEVICE "online" "$src_ip"
[ "$MWAN3_STARTUP" != 1 ] && mwan3_add_non_default_iface_route $INTERFACE $DEVICE
mwan3_set_iface_hotplug_state $INTERFACE "$binary_status"

mwan3_get_src_ip src_ip "$TRUE_INTERFACE"
if [ -n "${trackpid}" ]; then
device_pid=$(pgrep -f "mwan3track $INTERFACE $DEVICE ")
if [ "$device_pid" = "$trackpid" ]; then
[ "$ACTION" = ifup ] && kill -USR2 "$trackpid"
else
mwan3_track $INTERFACE $DEVICE "$binary_status" "$src_ip"
LOG notice "Restarted tracker [$!] on interface $INTERFACE (${DEVICE:-unknown})"
fi
else
$LOG notice "Starting tracker on interface $INTERFACE (${DEVICE:-unknown})"
mwan3_set_iface_hotplug_state $INTERFACE "offline"
mwan3_track $INTERFACE $DEVICE "offline" "$src_ip"
mwan3_track $INTERFACE $DEVICE "$binary_status" "$src_ip"
LOG notice "Started tracker [$!] on interface $INTERFACE (${DEVICE:-unknown})"
fi
mwan3_set_policies_iptables
mwan3_set_user_rules
[ "$MWAN3_STARTUP" != 1 ] && [ "$binary_status" == "online" ] && mwan3_set_policies_iptables

;;
ifdown)
ifdown|disconnected)
mwan3_set_iface_hotplug_state $INTERFACE "offline"
mwan3_delete_iface_ipset_entries $INTERFACE
mwan3_track_signal $INTERFACE $DEVICE
mwan3_delete_iface_rules $INTERFACE
mwan3_delete_iface_route $INTERFACE
mwan3_delete_iface_iptables $INTERFACE
if [ "$ACTION" = "ifdown" ]; then
[ -n "$trackpid" ] && kill -USR1 "$trackpid"
fi
mwan3_set_policies_iptables
mwan3_set_user_rules
;;
esac

mwan3_unlock "$ACTION" "$INTERFACE"

[ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$INTERFACE"
exit 0
8 changes: 4 additions & 4 deletions net/mwan3/files/etc/hotplug.d/iface/16-mwan3-user
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
. /lib/functions.sh
. /lib/mwan3/mwan3.sh

mwan3_lock "$ACTION" "user"
[ "$MWAN3_STARTUP" = 1 ] || mwan3_lock "$ACTION" "$DEVICE-user"

config_load mwan3
config_get_bool enabled globals 'enabled' '0'
[ "${enabled}" -gt 0 ] || {
mwan3_unlock "$ACTION" "user"
[ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$DEVICE-user"
exit 0
}

config_get_bool enabled "$INTERFACE" enabled 0
[ "${enabled}" -eq 1 ] || {
mwan3_unlock "$ACTION" "user"
[ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$DEVICE-user"
exit 0
}

mwan3_unlock "$ACTION" "user"
[ "$MWAN3_STARTUP" = 1 ] || mwan3_unlock "$ACTION" "$DEVICE-user"

env -i ACTION="$ACTION" INTERFACE="$INTERFACE" DEVICE="$DEVICE" \
/bin/sh /etc/mwan3.user
Expand Down
6 changes: 3 additions & 3 deletions net/mwan3/files/etc/init.d/mwan3
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ USE_PROCD=1

boot() {
. /lib/config/uci.sh
uci_toggle_state mwan3 globals enabled "1"
mwan3_boot=1
# disabled until mwan3 start runs so hotplug scripts
# do not start prematurely
uci_toggle_state mwan3 globals enabled "0"
rc_procd start_service
}

Expand All @@ -20,7 +21,6 @@ reload_service() {
}

start_service() {
[ -n "${mwan3_boot}" ] && return 0
/usr/sbin/mwan3 start 1000>&-
}

Expand Down
11 changes: 11 additions & 0 deletions net/mwan3/files/lib/mwan3/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ get_uptime() {
local uptime=$(cat /proc/uptime)
echo "${uptime%%.*}"
}

SCRIPTNAME="$(basename "$0")"
LOG()
{
local facility=$1; shift
# in development, we want to show 'debug' level logs
# when this release is out of beta, the comment in the line below
# should be removed
[ "$facility" = "debug" ] && return
logger -t "$SCRIPTNAME[$$]" -p $facility "$*"
}

0 comments on commit 36fcb67

Please sign in to comment.