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.

215 lines
9.0 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-4 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. ipt -t mangle -A QOS_MARK_${IFACE} -j MARK --set-mark 0x2${IPT_MASK_STRING}
  22. # You can go further with classification but...
  23. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class CS1 -j MARK --set-mark 0x3${IPT_MASK_STRING}
  24. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class CS6 -j MARK --set-mark 0x1${IPT_MASK_STRING}
  25. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class EF -j MARK --set-mark 0x1${IPT_MASK_STRING}
  26. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class AF42 -j MARK --set-mark 0x1${IPT_MASK_STRING}
  27. ipt -t mangle -A QOS_MARK_${IFACE} -m tos --tos Minimize-Delay -j MARK --set-mark 0x1${IPT_MASK_STRING}
  28. # and it might be a good idea to do it for udp tunnels too
  29. # Turn it on. Preserve classification if already performed
  30. if [ "$SQUASH_DSCP" = "1" ]
  31. then
  32. sqm_logger "Squashing differentiated services code points (DSCP) from ingress."
  33. ipt -t mangle -I PREROUTING -i $IFACE -m dscp ! --dscp 0 -j DSCP --set-dscp-class be
  34. else
  35. sqm_logger "Keeping differentiated services code points (DSCP) from ingress."
  36. ipt -t mangle -A PREROUTING -i $IFACE -m mark --mark 0x00${IPT_MASK_STRING} -g QOS_MARK_${IFACE}
  37. fi
  38. ipt -t mangle -A POSTROUTING -o $IFACE -m mark --mark 0x00${IPT_MASK_STRING} -g QOS_MARK_${IFACE}
  39. # The Syn optimization was nice but fq_codel does it for us
  40. # ipt -t mangle -A PREROUTING -i s+ -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j MARK --set-mark 0x01
  41. # Not sure if this will work. Encapsulation is a problem period
  42. ipt -t mangle -I PREROUTING -i vtun+ -p tcp -j MARK --set-mark 0x2${IPT_MASK_STRING} # tcp tunnels need ordering
  43. # Emanating from router, do a little more optimization
  44. # but don't bother with it too much.
  45. ipt -t mangle -A OUTPUT -p udp -m multiport --ports 123,53 -j DSCP --set-dscp-class AF42
  46. #Not clear if the second line is needed
  47. #ipt -t mangle -A OUTPUT -o $IFACE -g QOS_MARK_${IFACE}
  48. }
  49. # TC rules
  50. egress() {
  51. CEIL=${UPLINK}
  52. PRIO_RATE=`expr $CEIL / 3` # Ceiling for prioirty
  53. BE_RATE=`expr $CEIL / 6` # Min for best effort
  54. BK_RATE=`expr $CEIL / 6` # Min for background
  55. BE_CEIL=`expr $CEIL - 16` # A little slop at the top
  56. LQ="quantum `get_mtu $IFACE $CEIL`"
  57. $TC qdisc del dev $IFACE root 2> /dev/null
  58. $TC qdisc add dev $IFACE root handle 1: `get_stab_string` htb default 12
  59. $TC class add dev $IFACE parent 1: classid 1:1 htb $LQ rate ${CEIL}kbit ceil ${CEIL}kbit `get_htb_adsll_string`
  60. $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`
  61. $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`
  62. $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`
  63. $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`
  64. $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}
  65. $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}
  66. $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}
  67. # Need a catchall rule
  68. $TC filter add dev $IFACE parent 1:0 protocol all prio 999 u32 \
  69. match ip protocol 0 0x00 flowid 1:12
  70. # FIXME should probably change the filter here to do pre-nat
  71. $TC filter add dev $IFACE parent 1:0 protocol ip prio 1 handle 1 fw classid 1:11
  72. $TC filter add dev $IFACE parent 1:0 protocol ip prio 2 handle 2 fw classid 1:12
  73. $TC filter add dev $IFACE parent 1:0 protocol ip prio 3 handle 3 fw classid 1:13
  74. # ipv6 support. Note that the handle indicates the fw mark bucket that is looked for
  75. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 4 handle 1 fw classid 1:11
  76. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 5 handle 2 fw classid 1:12
  77. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 6 handle 3 fw classid 1:13
  78. # Arp traffic
  79. $TC filter add dev $IFACE parent 1:0 protocol arp prio 7 handle 1 fw classid 1:11
  80. # ICMP traffic - Don't impress your friends. Deoptimize to manage ping floods
  81. # better instead
  82. $TC filter add dev $IFACE parent 1:0 protocol ip prio 8 \
  83. u32 match ip protocol 1 0xff flowid 1:13
  84. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 9 \
  85. u32 match ip protocol 1 0xff flowid 1:13
  86. #diffserv $IFACE
  87. }
  88. ingress() {
  89. CEIL=$DOWNLINK
  90. PRIO_RATE=`expr $CEIL / 3` # Ceiling for prioirty
  91. BE_RATE=`expr $CEIL / 6` # Min for best effort
  92. BK_RATE=`expr $CEIL / 6` # Min for background
  93. BE_CEIL=`expr $CEIL - 16` # A little slop at the top
  94. LQ="quantum `get_mtu $IFACE $CEIL`"
  95. $TC qdisc del dev $IFACE handle ffff: ingress 2> /dev/null
  96. $TC qdisc add dev $IFACE handle ffff: ingress
  97. $TC qdisc del dev $DEV root 2> /dev/null
  98. if [ "$SQUASH_INGRESS" = "1" ]
  99. then
  100. sqm_logger "Do not perform DSCP based filtering on ingress. (1-tier classification)"
  101. # Revert to no dscp based filtering
  102. $TC qdisc del dev $DEV root 2>/dev/null
  103. $TC qdisc add dev $DEV root handle 1: `get_stab_string` htb default 10
  104. $TC class add dev $DEV parent 1: classid 1:1 htb $LQ rate ${DOWNLINK}kbit ceil ${DOWNLINK}kbit `get_htb_adsll_string`
  105. $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`
  106. $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}
  107. else
  108. sqm_logger "Perform DSCP based filtering on ingress. (3-tier classification)"
  109. $TC qdisc add dev $DEV root handle 1: `get_stab_string` htb default 12
  110. $TC class add dev $DEV parent 1: classid 1:1 htb $LQ rate ${CEIL}kbit ceil ${CEIL}kbit `get_htb_adsll_string`
  111. $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`
  112. $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`
  113. $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`
  114. $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`
  115. # I'd prefer to use a pre-nat filter but that causes permutation...
  116. $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}
  117. $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}
  118. $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}
  119. diffserv $DEV
  120. fi
  121. ifconfig $DEV up
  122. # redirect all IP packets arriving in $IFACE to ifb0
  123. $TC filter add dev $IFACE parent ffff: protocol all prio 10 u32 \
  124. match u32 0 0 flowid 1:1 action mirred egress redirect dev $DEV
  125. }
  126. do_modules
  127. ipt_setup
  128. if [ "$UPLINK" -ne 0 ];
  129. then
  130. egress
  131. sqm_logger "egress shaping activated"
  132. else
  133. sqm_logger "egress shaping deactivated"
  134. tc qdisc del dev $IFACE root 2> /dev/null
  135. fi
  136. if [ "$DOWNLINK" -ne 0 ];
  137. then
  138. ingress
  139. sqm_logger "ingress shaping activated"
  140. else
  141. sqm_logger "ingress shaping deactivated"
  142. tc qdisc del dev $DEV root 2> /dev/null
  143. tc qdisc del dev $IFACE ingress 2> /dev/null
  144. fi
  145. # References:
  146. # This alternate shaper attempts to go for 1/u performance in a clever way
  147. # http://git.coverfire.com/?p=linux-qos-scripts.git;a=blob;f=src-3tos.sh;hb=HEAD
  148. # Comments
  149. # This does the right thing with ipv6 traffic.
  150. # It also tries to leverage diffserv to some sane extent. In particular,
  151. # the 'priority' queue is limited to 33% of the total, so EF, and IMM traffic
  152. # cannot starve other types. The rfc suggested 30%. 30% is probably
  153. # a lot in today's world.
  154. # Flaws
  155. # Many!