run-openwrt.sh 4.3 KB

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