You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.5 KiB

  1. #!/bin/sh
  2. # Wonder Shaper
  3. # please read the README before filling out these values
  4. #
  5. # Set the following values to somewhat less than your actual download
  6. # and uplink speed. In kilobits. Also set the device that is to be shaped.
  7. # All config needs to be done in /etc/config/wshaper
  8. . /lib/functions.sh
  9. . /lib/functions/network.sh
  10. config_load wshaper
  11. for s in downlink uplink network nopriohostdst nopriohostsrc noprioportdst noprioportsrc; do
  12. config_get $s settings $s
  13. done
  14. if ! network_get_device device "$network"; then
  15. logger -t wondershaper "Error: Could not find the device for network $network, aborting."
  16. exit 1
  17. fi
  18. [ -z "$downlink" ] && logger -t wondershaper "Error: Downlink speed not set, aborting." && exit 1
  19. [ -z "$uplink" ] && logger -t wondershaper "Error: Uplink speed not set, aborting." && exit 1
  20. MODULES='sch_ingress sch_sfq sch_htb cls_u32 act_police'
  21. DOWNLINK="$downlink"
  22. UPLINK="$uplink"
  23. DEV="$device"
  24. # low priority OUTGOING traffic - you can leave this blank if you want
  25. # low priority source netmasks
  26. NOPRIOHOSTSRC="$nopriohostsrc"
  27. # low priority destination netmasks
  28. NOPRIOHOSTDST="$nopriohostdst"
  29. # low priority source ports
  30. NOPRIOPORTSRC="$noprioportsrc"
  31. # low priority destination ports
  32. NOPRIOPORTDST="$noprioportdst"
  33. if [ "$1" = "status" ]
  34. then
  35. tc -s qdisc ls dev $DEV
  36. tc -s class ls dev $DEV
  37. exit
  38. fi
  39. # clean existing down- and uplink qdiscs, hide errors
  40. tc qdisc del dev $DEV root 2> /dev/null > /dev/null
  41. tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null
  42. if [ "$1" = "stop" ]
  43. then
  44. for i in $MODULES ; do
  45. rmmod $i
  46. done
  47. exit
  48. fi
  49. for i in $MODULES ; do
  50. insmod $i
  51. done
  52. ###### uplink
  53. # install root HTB, point default traffic to 1:20:
  54. tc qdisc add dev $DEV root handle 1: htb default 20
  55. # shape everything at $UPLINK speed - this prevents huge queues in your
  56. # DSL modem which destroy latency:
  57. tc class add dev $DEV parent 1: classid 1:1 htb rate ${UPLINK}kbit burst 6k
  58. # high prio class 1:10:
  59. tc class add dev $DEV parent 1:1 classid 1:10 htb rate ${UPLINK}kbit \
  60. burst 6k prio 1
  61. # bulk & default class 1:20 - gets slightly less traffic,
  62. # and a lower priority:
  63. tc class add dev $DEV parent 1:1 classid 1:20 htb rate $((9*$UPLINK/10))kbit \
  64. burst 6k prio 2
  65. tc class add dev $DEV parent 1:1 classid 1:30 htb rate $((8*$UPLINK/10))kbit \
  66. burst 6k prio 2
  67. # all get Stochastic Fairness:
  68. tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
  69. tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
  70. tc qdisc add dev $DEV parent 1:30 handle 30: sfq perturb 10
  71. # TOS Minimum Delay (ssh, NOT scp) in 1:10:
  72. tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
  73. match ip tos 0x10 0xff flowid 1:10
  74. # ICMP (ip protocol 1) in the interactive class 1:10 so we
  75. # can do measurements & impress our friends:
  76. tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
  77. match ip protocol 1 0xff flowid 1:10
  78. # To speed up downloads while an upload is going on, put ACK packets in
  79. # the interactive class:
  80. tc filter add dev $DEV parent 1: protocol ip prio 10 u32 \
  81. match ip protocol 6 0xff \
  82. match u8 0x05 0x0f at 0 \
  83. match u16 0x0000 0xffc0 at 2 \
  84. match u8 0x10 0xff at 33 \
  85. flowid 1:10
  86. # rest is 'non-interactive' ie 'bulk' and ends up in 1:20
  87. # some traffic however suffers a worse fate
  88. for a in $NOPRIOPORTDST
  89. do
  90. tc filter add dev $DEV parent 1: protocol ip prio 14 u32 \
  91. match ip dport $a 0xffff flowid 1:30
  92. done
  93. for a in $NOPRIOPORTSRC
  94. do
  95. tc filter add dev $DEV parent 1: protocol ip prio 15 u32 \
  96. match ip sport $a 0xffff flowid 1:30
  97. done
  98. for a in $NOPRIOHOSTSRC
  99. do
  100. tc filter add dev $DEV parent 1: protocol ip prio 16 u32 \
  101. match ip src $a flowid 1:30
  102. done
  103. for a in $NOPRIOHOSTDST
  104. do
  105. tc filter add dev $DEV parent 1: protocol ip prio 17 u32 \
  106. match ip dst $a flowid 1:30
  107. done
  108. # rest is 'non-interactive' ie 'bulk' and ends up in 1:20
  109. tc filter add dev $DEV parent 1: protocol ip prio 18 u32 \
  110. match ip dst 0.0.0.0/0 flowid 1:20
  111. ########## downlink #############
  112. # slow downloads down to somewhat less than the real speed to prevent
  113. # queuing at our ISP. Tune to see how high you can set it.
  114. # ISPs tend to have *huge* queues to make sure big downloads are fast
  115. #
  116. # attach ingress policer:
  117. tc qdisc add dev $DEV handle ffff: ingress
  118. # filter *everything* to it (0.0.0.0/0), drop everything that's
  119. # coming in too fast:
  120. tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
  121. 0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1
  122. logger -t wondershaper "Wondershaper was started on device $device."