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.

245 lines
10 KiB

  1. #!/bin/sh
  2. # Cero3 Shaper
  3. # A 3 bin tc_codel and ipv6 enabled shaping script for
  4. # ethernet gateways
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # Copyright (C) 2012-5 Michael D. Taht, Toke Høiland-Jørgensen, Sebastian Moeller
  10. # Compared to the complexity that debloat had become
  11. # this cleanly shows a means of going from diffserv marking
  12. # to prioritization using the current tools (ip(6)tables
  13. # and tc. I note that the complexity of debloat exists for
  14. # a reason, and it is expected that script is run first
  15. # to setup various other parameters such as BQL and ethtool.
  16. # (And that the debloat script has setup the other interfaces)
  17. # You need to jiggle these parameters. Note limits are tuned towards a <10Mbit uplink <60Mbup down
  18. . /usr/lib/sqm/functions.sh
  19. ipt_setup() {
  20. ipt -t mangle -N QOS_MARK_${IFACE}
  21. case $QDISC in
  22. cake*)
  23. sqm_logger cake does all the diffserv work - no need for iptables rules
  24. ;;
  25. *)
  26. ipt -t mangle -A QOS_MARK_${IFACE} -j MARK --set-mark 0x2${IPT_MASK_STRING}
  27. # You can go further with classification but...
  28. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class CS1 -j MARK --set-mark 0x3${IPT_MASK_STRING}
  29. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class CS6 -j MARK --set-mark 0x1${IPT_MASK_STRING}
  30. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class EF -j MARK --set-mark 0x1${IPT_MASK_STRING}
  31. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class AF42 -j MARK --set-mark 0x1${IPT_MASK_STRING}
  32. ipt -t mangle -A QOS_MARK_${IFACE} -m tos --tos Minimize-Delay -j MARK --set-mark 0x1${IPT_MASK_STRING}
  33. ;;
  34. esac
  35. # and it might be a good idea to do it for udp tunnels too
  36. # Turn it on. Preserve classification if already performed
  37. #sm: is it correct to do this in $IFACE? Should ingress not be on $DEV? since HTB acts on $DEV?
  38. # SQUASH also does not work on $DEV (that is the IFB will still see the incoming ToS bits whether we squash or not)
  39. # SQUASH is still useful to protect internal machines...
  40. if [ "$SQUASH_DSCP" = "1" ]
  41. then
  42. sqm_logger "Squashing differentiated services code points (DSCP) from ingress."
  43. CAKE_OPTS=besteffort # someday squash
  44. ipt -t mangle -I PREROUTING -i $IFACE -m dscp ! --dscp 0 -j DSCP --set-dscp-class be
  45. else
  46. sqm_logger "Keeping differentiated services code points (DSCP) from ingress."
  47. CAKE_OPTS=""
  48. ipt -t mangle -A PREROUTING -i $IFACE -m mark --mark 0x00${IPT_MASK_STRING} -g QOS_MARK_${IFACE}
  49. fi
  50. ipt -t mangle -A POSTROUTING -o $IFACE -m mark --mark 0x00${IPT_MASK_STRING} -g QOS_MARK_${IFACE}
  51. # The Syn optimization was nice but fq_codel does it for us
  52. # ipt -t mangle -A PREROUTING -i s+ -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j MARK --set-mark 0x01
  53. # Not sure if this will work. Encapsulation is a problem period
  54. ipt -t mangle -I PREROUTING -i vtun+ -p tcp -j MARK --set-mark 0x2${IPT_MASK_STRING} # tcp tunnels need ordering
  55. # Emanating from router, do a little more optimization
  56. # but don't bother with it too much.
  57. ipt -t mangle -A OUTPUT -p udp -m multiport --ports 123,53 -j DSCP --set-dscp-class AF42
  58. #Not clear if the second line is needed
  59. #ipt -t mangle -A OUTPUT -o $IFACE -g QOS_MARK_${IFACE}
  60. }
  61. # TC rules
  62. egress() {
  63. CEIL=${UPLINK}
  64. PRIO_RATE=`expr $CEIL / 3` # Ceiling for prioirty
  65. BE_RATE=`expr $CEIL / 6` # Min for best effort
  66. BK_RATE=`expr $CEIL / 6` # Min for background
  67. BE_CEIL=`expr $CEIL - 16` # A little slop at the top
  68. LQ="quantum `get_mtu $IFACE $CEIL`"
  69. $TC qdisc del dev $IFACE root 2> /dev/null
  70. case $QDISC in
  71. cake*)
  72. $TC qdisc add dev $IFACE root `get_stab_string` $QDISC bandwidth ${CEIL}kbit `get_cake_lla_string` ${EQDISC_OPTS}
  73. ;;
  74. *)
  75. $TC qdisc add dev $IFACE root handle 1: `get_stab_string` htb default 12
  76. $TC class add dev $IFACE parent 1: classid 1:1 htb $LQ rate ${CEIL}kbit ceil ${CEIL}kbit `get_htb_adsll_string`
  77. $TC class add dev $IFACE parent 1:1 classid 1:10 htb $LQ rate ${CEIL}kbit ceil ${CEIL}kbit prio 0 `get_htb_adsll_string`
  78. $TC class add dev $IFACE parent 1:1 classid 1:11 htb $LQ rate 128kbit ceil ${PRIO_RATE}kbit prio 1 `get_htb_adsll_string`
  79. $TC class add dev $IFACE parent 1:1 classid 1:12 htb $LQ rate ${BE_RATE}kbit ceil ${BE_CEIL}kbit prio 2 `get_htb_adsll_string`
  80. $TC class add dev $IFACE parent 1:1 classid 1:13 htb $LQ rate ${BK_RATE}kbit ceil ${BE_CEIL}kbit prio 3 `get_htb_adsll_string`
  81. $TC qdisc add dev $IFACE parent 1:11 handle 110: $QDISC `get_limit ${ELIMIT}` `get_target "${ETARGET}" ${UPLINK}` `get_ecn ${EECN}` `get_quantum 300` `get_flows ${PRIO_RATE}` ${EQDISC_OPTS}
  82. $TC qdisc add dev $IFACE parent 1:12 handle 120: $QDISC `get_limit ${ELIMIT}` `get_target "${ETARGET}" ${UPLINK}` `get_ecn ${EECN}` `get_quantum 300` `get_flows ${BE_RATE}` ${EQDISC_OPTS}
  83. $TC qdisc add dev $IFACE parent 1:13 handle 130: $QDISC `get_limit ${ELIMIT}` `get_target "${ETARGET}" ${UPLINK}` `get_ecn ${EECN}` `get_quantum 300` `get_flows ${BK_RATE}` ${EQDISC_OPTS}
  84. # Need a catchall rule
  85. $TC filter add dev $IFACE parent 1:0 protocol all prio 999 u32 \
  86. match ip protocol 0 0x00 flowid 1:12
  87. # FIXME should probably change the filter here to do pre-nat
  88. $TC filter add dev $IFACE parent 1:0 protocol ip prio 1 handle 1 fw classid 1:11
  89. $TC filter add dev $IFACE parent 1:0 protocol ip prio 2 handle 2 fw classid 1:12
  90. $TC filter add dev $IFACE parent 1:0 protocol ip prio 3 handle 3 fw classid 1:13
  91. # ipv6 support. Note that the handle indicates the fw mark bucket that is looked for
  92. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 4 handle 1 fw classid 1:11
  93. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 5 handle 2 fw classid 1:12
  94. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 6 handle 3 fw classid 1:13
  95. # Arp traffic
  96. $TC filter add dev $IFACE parent 1:0 protocol arp prio 7 handle 1 fw classid 1:11
  97. # ICMP traffic - Don't impress your friends. Deoptimize to manage ping floods
  98. # better instead
  99. $TC filter add dev $IFACE parent 1:0 protocol ip prio 8 \
  100. u32 match ip protocol 1 0xff flowid 1:13
  101. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 9 \
  102. u32 match ip protocol 1 0xff flowid 1:13
  103. ;;
  104. esac
  105. #diffserv $IFACE
  106. }
  107. ingress() {
  108. CEIL=$DOWNLINK
  109. PRIO_RATE=`expr $CEIL / 3` # Ceiling for prioirty
  110. BE_RATE=`expr $CEIL / 6` # Min for best effort
  111. BK_RATE=`expr $CEIL / 6` # Min for background
  112. BE_CEIL=`expr $CEIL - 16` # A little slop at the top
  113. LQ="quantum `get_mtu $IFACE $CEIL`"
  114. $TC qdisc del dev $IFACE handle ffff: ingress 2> /dev/null
  115. $TC qdisc add dev $IFACE handle ffff: ingress
  116. $TC qdisc del dev $DEV root 2> /dev/null
  117. if [ "$SQUASH_INGRESS" = "1" ]
  118. then
  119. sqm_logger "Do not perform DSCP based filtering on ingress. (1-tier classification)"
  120. # Revert to no dscp based filtering
  121. case $QDISC in
  122. cake*)
  123. $TC qdisc add dev $DEV root `get_stab_string` $QDISC bandwidth ${DOWNLINK}kbit besteffort `get_cake_lla_string` ${IQDISC_OPTS}
  124. ;;
  125. *)
  126. $TC qdisc add dev $DEV root handle 1: `get_stab_string` htb default 10
  127. $TC class add dev $DEV parent 1: classid 1:1 htb $LQ rate ${DOWNLINK}kbit ceil ${DOWNLINK}kbit `get_htb_adsll_string`
  128. $TC class add dev $DEV parent 1:1 classid 1:10 htb $LQ rate ${DOWNLINK}kbit ceil ${DOWNLINK}kbit prio 0 `get_htb_adsll_string`
  129. $TC qdisc add dev $DEV parent 1:10 handle 110: $QDISC `get_limit ${ILIMIT}` `get_target "${ITARGET}" ${DOWNLINK}` `get_ecn ${IECN}` `get_flows ${DOWNLINK}` ${IQDISC_OPTS}
  130. ;;
  131. esac
  132. else
  133. sqm_logger "Perform DSCP based filtering on ingress. (3-tier classification)"
  134. case $QDISC in
  135. cake*)
  136. $TC qdisc add dev $DEV root `get_stab_string` $QDISC bandwidth ${DOWNLINK}kbit `get_cake_lla_string` $CAKE_OPTS ${IQDISC_OPTS}
  137. ;;
  138. *)
  139. $TC qdisc add dev $DEV root handle 1: `get_stab_string` htb default 12
  140. $TC class add dev $DEV parent 1: classid 1:1 htb $LQ rate ${CEIL}kbit ceil ${CEIL}kbit `get_htb_adsll_string`
  141. $TC class add dev $DEV parent 1:1 classid 1:10 htb $LQ rate ${CEIL}kbit ceil ${CEIL}kbit prio 0 `get_htb_adsll_string`
  142. $TC class add dev $DEV parent 1:1 classid 1:11 htb $LQ rate 32kbit ceil ${PRIO_RATE}kbit prio 1 `get_htb_adsll_string`
  143. $TC class add dev $DEV parent 1:1 classid 1:12 htb $LQ rate ${BE_RATE}kbit ceil ${BE_CEIL}kbit prio 2 `get_htb_adsll_string`
  144. $TC class add dev $DEV parent 1:1 classid 1:13 htb $LQ rate ${BK_RATE}kbit ceil ${BE_CEIL}kbit prio 3 `get_htb_adsll_string`
  145. # I'd prefer to use a pre-nat filter but that causes permutation...
  146. $TC qdisc add dev $DEV parent 1:11 handle 110: $QDISC `get_limit ${ILIMIT}` `get_target "${ITARGET}" ${DOWNLINK}` `get_ecn ${IECN}` `get_quantum 500` `get_flows ${PRIO_RATE}` ${IQDISC_OPTS}
  147. $TC qdisc add dev $DEV parent 1:12 handle 120: $QDISC `get_limit ${ILIMIT}` `get_target "${ITARGET}" ${DOWNLINK}` `get_ecn ${IECN}` `get_quantum 1500` `get_flows ${BE_RATE}` ${IQDISC_OPTS}
  148. $TC qdisc add dev $DEV parent 1:13 handle 130: $QDISC `get_limit ${ILIMIT}` `get_target "${ITARGET}" ${DOWNLINK}` `get_ecn ${IECN}` `get_quantum 300` `get_flows ${BK_RATE}` ${IQDISC_OPTS}
  149. diffserv $DEV
  150. ;;
  151. esac
  152. fi
  153. ifconfig $DEV up
  154. # redirect all IP packets arriving in $IFACE to ifb0
  155. $TC filter add dev $IFACE parent ffff: protocol all prio 10 u32 \
  156. match u32 0 0 flowid 1:1 action mirred egress redirect dev $DEV
  157. }
  158. do_modules
  159. ipt_setup
  160. if [ "$UPLINK" -ne 0 ];
  161. then
  162. egress
  163. sqm_logger "egress shaping activated"
  164. else
  165. sqm_logger "egress shaping deactivated"
  166. tc qdisc del dev $IFACE root 2> /dev/null
  167. fi
  168. if [ "$DOWNLINK" -ne 0 ];
  169. then
  170. ingress
  171. sqm_logger "ingress shaping activated"
  172. else
  173. sqm_logger "ingress shaping deactivated"
  174. tc qdisc del dev $DEV root 2> /dev/null
  175. tc qdisc del dev $IFACE ingress 2> /dev/null
  176. fi
  177. # References:
  178. # This alternate shaper attempts to go for 1/u performance in a clever way
  179. # http://git.coverfire.com/?p=linux-qos-scripts.git;a=blob;f=src-3tos.sh;hb=HEAD
  180. # Comments
  181. # This does the right thing with ipv6 traffic.
  182. # It also tries to leverage diffserv to some sane extent. In particular,
  183. # the 'priority' queue is limited to 33% of the total, so EF, and IMM traffic
  184. # cannot starve other types. The rfc suggested 30%. 30% is probably
  185. # a lot in today's world.
  186. # Flaws
  187. # Many!