run-openwrt.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/sh
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License version 2 as
  4. # published by the Free Software Foundation.
  5. #
  6. # Copyright (C) 2012-4 Michael D. Taht, Toke Høiland-Jørgensen, Sebastian Moeller
  7. . /lib/functions.sh
  8. . /etc/sqm/sqm.conf
  9. . ${SQM_LIB_DIR}/functions.sh
  10. ACTION="${1:-start}"
  11. RUN_IFACE="$2"
  12. LOCKDIR="${SQM_STATE_DIR}/sqm-run.lock"
  13. CLEANUP=0
  14. if [ "$ACTION" = "cleanup" ]; then
  15. CLEANUP=1
  16. ACTION=stop
  17. fi
  18. check_state_dir
  19. [ -d "${SQM_QDISC_STATE_DIR}" ] || ${SQM_LIB_DIR}/update-available-qdiscs
  20. stop_statefile() {
  21. local f
  22. f="$1"
  23. # Source the state file prior to stopping; we need the variables saved in
  24. # there.
  25. [ -f "$f" ] && ( . "$f";
  26. IFACE=$IFACE SCRIPT=$SCRIPT SQM_DEBUG=$SQM_DEBUG \
  27. SQM_DEBUG_LOG=$SQM_DEBUG_LOG CLEANUP=$CLEANUP \
  28. SQM_VERBOSITY_MAX=$SQM_VERBOSITY_MAX \
  29. SQM_VERBOSITY_MIN=$SQM_VERBOSITY_MIN \
  30. OUTPUT_TARGET=$OUTPUT_TARGET ${SQM_LIB_DIR}/stop-sqm )
  31. }
  32. start_sqm_section() {
  33. local section
  34. section="$1"
  35. export IFACE=$(config_get "$section" interface)
  36. [ -z "$RUN_IFACE" -o "$RUN_IFACE" = "$IFACE" ] || return
  37. [ "$(config_get "$section" enabled)" -eq 1 ] || return 0
  38. [ -f "${SQM_STATE_DIR}/${IFACE}.state" ] && return
  39. export UPLINK=$(config_get "$section" upload)
  40. export DOWNLINK=$(config_get "$section" download)
  41. export LLAM=$(config_get "$section" linklayer_adaptation_mechanism)
  42. export LINKLAYER=$(config_get "$section" linklayer)
  43. export OVERHEAD=$(config_get "$section" overhead)
  44. export STAB_MTU=$(config_get "$section" tcMTU)
  45. export STAB_TSIZE=$(config_get "$section" tcTSIZE)
  46. export STAB_MPU=$(config_get "$section" tcMPU)
  47. export ILIMIT=$(config_get "$section" ilimit)
  48. export ELIMIT=$(config_get "$section" elimit)
  49. export ITARGET=$(config_get "$section" itarget)
  50. export ETARGET=$(config_get "$section" etarget)
  51. export IECN=$(config_get "$section" ingress_ecn)
  52. export EECN=$(config_get "$section" egress_ecn)
  53. export IQDISC_OPTS=$(config_get "$section" iqdisc_opts)
  54. export EQDISC_OPTS=$(config_get "$section" eqdisc_opts)
  55. export TARGET=$(config_get "$section" target)
  56. export QDISC=$(config_get "$section" qdisc)
  57. export SCRIPT=$(config_get "$section" script)
  58. # The UCI names for these two variables are confusing and should have been
  59. # changed ages ago. For now, keep the bad UCI names but use meaningful
  60. # variable names in the scripts to not break user configs.
  61. export ZERO_DSCP_INGRESS=$(config_get "$section" squash_dscp)
  62. export IGNORE_DSCP_INGRESS=$(config_get "$section" squash_ingress)
  63. # If SQM_DEBUG or SQM_VERBOSITY_* were passed in via the command line make
  64. # them available to the other scripts this allows to override sqm's log
  65. # level as set in the GUI for quick debugging without GUI accesss.
  66. export SQM_DEBUG=${SQM_DEBUG:-$(config_get "$section" debug_logging)}
  67. export SQM_VERBOSITY_MAX=${SQM_VERBOSITY_MAX:-$(config_get "$section" verbosity)}
  68. export SQM_VERBOSITY_MIN
  69. "${SQM_LIB_DIR}/start-sqm"
  70. }
  71. release_lock() {
  72. PID=$(cat "$LOCKDIR/pid")
  73. if [ "$PID" -ne "$$" ]; then
  74. sqm_error "Trying to release lock with wrong PID $PID != $$"
  75. return 1
  76. fi
  77. rm -rf "$LOCKDIR"
  78. return 0
  79. }
  80. take_lock() {
  81. if mkdir "$LOCKDIR" 2>/dev/null; then
  82. sqm_trace "Acquired run lock"
  83. echo $$ > "$LOCKDIR/pid"
  84. trap release_lock 0
  85. return 0
  86. fi
  87. PID=$(cat "$LOCKDIR/pid")
  88. sqm_warn "Unable to get run lock - already held by $PID"
  89. return 1
  90. }
  91. MAX_TRIES=10
  92. tries=$MAX_TRIES
  93. while ! take_lock; do
  94. sleep 1
  95. tries=$((tries - 1))
  96. if [ "$tries" -eq 0 ]; then
  97. sqm_error "Giving up on getting lock after $MAX_TRIES attempts"
  98. sqm_error "This is a bug; please report it at https://github.com/tohojo/sqm-scripts/issues"
  99. sqm_error "Then, to re-enable sqm-scripts, manually remove $LOCKDIR"
  100. exit 1
  101. fi
  102. done
  103. if [ "$ACTION" = "stop" ]; then
  104. if [ -z "$RUN_IFACE" ]; then
  105. # Stopping all active interfaces
  106. for f in ${SQM_STATE_DIR}/*.state; do
  107. stop_statefile "$f"
  108. done
  109. else
  110. stop_statefile "${SQM_STATE_DIR}/${RUN_IFACE}.state"
  111. fi
  112. else
  113. config_load sqm
  114. config_foreach start_sqm_section
  115. fi