b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh /etc/rc.common |
| 2 | |
| 3 | # shellcheck disable=SC2034 # foo appears unused. Verify it or export it. |
| 4 | |
| 5 | START=25 |
| 6 | STOP=99 |
| 7 | |
| 8 | MSTPCTL="/usr/sbin/mstpctl" |
| 9 | MSTPD="/usr/sbin/mstpd" |
| 10 | |
| 11 | USE_PROCD=1 |
| 12 | |
| 13 | mstpd_get_bridges() { |
| 14 | "$MSTPCTL" showbridge | grep -v "^ " | cut -d " " -f 1 2>/dev/null |
| 15 | } |
| 16 | |
| 17 | # mstpd log levels |
| 18 | # LOG_LEVEL_NONE 0 |
| 19 | # LOG_LEVEL_ERROR 1 |
| 20 | # LOG_LEVEL_INFO 2 |
| 21 | # LOG_LEVEL_DEBUG 3 |
| 22 | # LOG_LEVEL_STATE_MACHINE_TRANSITION 4 |
| 23 | # LOG_LEVEL_MAX 100 |
| 24 | |
| 25 | config_bridge_port_mstpd() { |
| 26 | local config="$1" |
| 27 | local index=$2 # FIXME: maybe remove index later |
| 28 | local name=$3 |
| 29 | |
| 30 | [ -n "$index" -a -n "$name" ] || return 0 |
| 31 | |
| 32 | config_get br_index "$config" br_index |
| 33 | [ -n "$br_index" ] || return 0 |
| 34 | [ "$index" = "$br_index" ] || return 0 |
| 35 | |
| 36 | config_get port_name "$config" name |
| 37 | [ -n "$port_name" ] || return 0 |
| 38 | |
| 39 | for opt in bpduguard; do |
| 40 | config_get $opt "$config" $opt |
| 41 | eval optval=\$$opt |
| 42 | [ -z "$optval" ] || "$MSTPCTL" "set$opt" "$name" "$port_name" "$optval" |
| 43 | done |
| 44 | } |
| 45 | |
| 46 | config_bridge_mstpd() { |
| 47 | local config="$1" |
| 48 | local optval= |
| 49 | local name= |
| 50 | local enable= |
| 51 | local mstid=0 # for the moment, using only MSTID |
| 52 | |
| 53 | config_get index "$config" index |
| 54 | [ -n "$index" ] || return 1 |
| 55 | |
| 56 | # Get bridge name |
| 57 | config_get name "$config" name |
| 58 | [ -n "$name" ] || return 0 |
| 59 | |
| 60 | config_get enable "$config" enable |
| 61 | if [ "$enable" != "1" ] ; then |
| 62 | return 0 |
| 63 | fi |
| 64 | |
| 65 | list_contains MSTPD_PREINSTALLED_BRIDGES "$name" || \ |
| 66 | "$MSTPCTL" addbridge "$name" |
| 67 | # All options here have 'set$opt' equivalent calls in mstpd, |
| 68 | # hence this trick with the loop |
| 69 | for opt in maxage fdelay maxhops hello ageing forcevers txholdcount; do |
| 70 | config_get $opt "$config" "$opt" |
| 71 | eval optval=\$$opt |
| 72 | [ -z "$optval" ] || "$MSTPCTL" set$opt "$name" "$optval" |
| 73 | done |
| 74 | config_get treeprio "$config" treeprio |
| 75 | [ -z "$treeprio" ] || $MSTPCTL settreeprio "$name" "$mstid" "$treeprio" |
| 76 | config_foreach config_bridge_port_mstpd bridge_port "$index" "$name" |
| 77 | CONFIGURED_BRIDGES="$CONFIGURED_BRIDGES $name" |
| 78 | export CONFIGURED_BRIDGES |
| 79 | } |
| 80 | |
| 81 | start_service() { |
| 82 | procd_open_instance |
| 83 | procd_set_param command $MSTPD |
| 84 | procd_append_param command -v 2 |
| 85 | procd_append_param command -d # don't daemonize, procd will handle that for us |
| 86 | procd_append_param command -s # print to syslog |
| 87 | |
| 88 | # set auto respawn behavior |
| 89 | procd_set_param respawn |
| 90 | |
| 91 | # reload config on respawn |
| 92 | procd_open_trigger |
| 93 | procd_add_raw_trigger "instance.start" 2000 "/etc/init.d/mstpd" "reload" |
| 94 | procd_close_trigger |
| 95 | |
| 96 | procd_close_instance |
| 97 | } |
| 98 | |
| 99 | service_running() { |
| 100 | pgrep mstpd >/dev/null 2>&1 |
| 101 | } |
| 102 | |
| 103 | reload_service() { |
| 104 | if ! running ; then |
| 105 | start |
| 106 | return |
| 107 | fi |
| 108 | |
| 109 | unset CONFIGURED_BRIDGES |
| 110 | MSTPD_PREINSTALLED_BRIDGES="$(mstpd_get_bridges)" |
| 111 | export MSTPD_PREINSTALLED_BRIDGES |
| 112 | |
| 113 | config_load 'mstpd' |
| 114 | config_foreach config_bridge_mstpd bridge |
| 115 | |
| 116 | for bridge in $(mstpd_get_bridges) ; do |
| 117 | list_contains CONFIGURED_BRIDGES "$bridge" || \ |
| 118 | $MSTPCTL delbridge "$bridge" |
| 119 | done |
| 120 | # return 0 (success) here, otherwise, and endless restart loop will occur from procd |
| 121 | # because the last return code may be mstpctl failing |
| 122 | return 0 |
| 123 | } |
| 124 | |