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.

390 lines
15 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
  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
  24. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class CS6 -j MARK --set-mark 0x1
  25. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class EF -j MARK --set-mark 0x1
  26. ipt -t mangle -A QOS_MARK_${IFACE} -m dscp --dscp-class AF42 -j MARK --set-mark 0x1
  27. ipt -t mangle -A QOS_MARK_${IFACE} -m tos --tos Minimize-Delay -j MARK --set-mark 0x1
  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 differentiad 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 differentiad services code points (DSCP) from ingress."
  36. ipt -t mangle -A PREROUTING -i $IFACE -m mark --mark 0x00 -g QOS_MARK_${IFACE}
  37. fi
  38. ipt -t mangle -A POSTROUTING -o $IFACE -m mark --mark 0x00 -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 # 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. #sm: for testing we need a band to collect PPPOEd packets
  68. $TC class add dev $IFACE parent 1:1 classid 1:14 htb $LQ rate ${BE_RATE}kbit ceil ${BE_CEIL}kbit prio 2 `get_htb_adsll_string`
  69. $TC qdisc add dev $IFACE parent 1:14 handle 140: $QDISC `get_limit ${ELIMIT}` `get_target "${ETARGET}" ${UPLINK}` `get_ecn ${EECN}` `get_quantum 300` `get_flows ${BK_RATE}` ${EQDISC_OPTS}
  70. # Need a catchall rule (should also match VLANs and PPPoE packets)
  71. $TC filter add dev $IFACE parent 1:0 protocol all prio 999 u32 \
  72. match ip protocol 0 0x00 flowid 1:12
  73. # FIXME should probably change the filter here to do pre-nat
  74. $TC filter add dev $IFACE parent 1:0 protocol ip prio 1 handle 1 fw classid 1:11
  75. $TC filter add dev $IFACE parent 1:0 protocol ip prio 2 handle 2 fw classid 1:12
  76. $TC filter add dev $IFACE parent 1:0 protocol ip prio 3 handle 3 fw classid 1:13
  77. # ipv6 support. Note that the handle indicates the fw mark bucket that is looked for
  78. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 4 handle 1 fw classid 1:11
  79. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 5 handle 2 fw classid 1:12
  80. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 6 handle 3 fw classid 1:13
  81. # Arp traffic
  82. $TC filter add dev $IFACE parent 1:0 protocol arp prio 7 handle 1 fw classid 1:11
  83. # ICMP traffic - Don't impress your friends. Deoptimize to manage ping floods
  84. # better instead
  85. $TC filter add dev $IFACE parent 1:0 protocol ip prio 8 \
  86. u32 match ip protocol 1 0xff flowid 1:13
  87. $TC filter add dev $IFACE parent 1:0 protocol ipv6 prio 9 \
  88. u32 match ip protocol 1 0xff flowid 1:13
  89. # PPPoE encapsulated packets traversing the router (e.g.: the router does PPPoE termination but we shape
  90. # on the underlaying ethernet interface instead of the pppoe device)
  91. PPPOE_SESSION_ETHERTYPE="0x8864"
  92. PPPOE_DISCOVERY_ETHERTYPE="0x8863"
  93. PPP_PROTO_IP4="0x0021"
  94. PPP_PROTO_IP6="0x0057"
  95. ARP_PROTO_IP4="0x0806"
  96. # NOTE it seems prio can not be reused?
  97. #$TC filter add dev $IFACE protocol 0x8863 parent 1:0 prio 1 u32 flowid 1:14
  98. # PPPoE can be selected for by ether_type, the encapsulated IP version from the PPP (0x0021 IPv4, 0x0057 IPv6)
  99. #U32_PREFIX="$TC filter add dev $IFACE" parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE}"
  100. #BE: 1:12 is the default anyway, but this will catch all non marked packets
  101. #$TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 400 u32 \
  102. # match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  103. # match u8 0x00 0xfb at 9 \
  104. # flowid 1:12
  105. #AF42
  106. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 401 u32 \
  107. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  108. match u8 0x90 0xfc at 9 \
  109. flowid 1:11
  110. #EF
  111. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 402 u32 \
  112. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  113. match u8 0xb8 0xfc at 9 \
  114. flowid 1:11
  115. #CS1
  116. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 403 u32 \
  117. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  118. match u8 0x20 0xf0 at 9 \
  119. flowid 1:13
  120. #IMM
  121. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 404 u32 \
  122. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  123. match u8 0x10 0xf0 at 9 \
  124. flowid 1:11
  125. #CS3
  126. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 405 u32 \
  127. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  128. match u8 0xc0 0xf0 at 9 \
  129. flowid 1:11
  130. #CS6
  131. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 406 u32 \
  132. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  133. match u8 0xe0 0xf0 at 9 \
  134. flowid 1:11
  135. ## Arp traffic
  136. #$TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 409 u32 \
  137. # match u16 ${ARP_PROTO_IP4} 0xffff at 6 \
  138. # flowid 1:14
  139. # ICMP traffic - Don't impress your friends. Deoptimize to manage ping floods
  140. # better instead; sm: really only deprio echo requestst and echo replies instead?
  141. # ECHO request, the rest stays in best effort
  142. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 410 u32 \
  143. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  144. match u8 0x01 0xff at 17 \
  145. match u8 0x08 0xff at 28 \
  146. flowid 1:13
  147. # ECHO reply
  148. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 410 u32 \
  149. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  150. match u8 0x01 0xff at 17 \
  151. match u8 0x00 0xff at 28 \
  152. flowid 1:13
  153. ## ICMPv6 133-137 (NDP) is equivalent to IPv4 ARP, so only push echo request and reply into the bulk class
  154. ## 133
  155. #$TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 610 u32 \
  156. # match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  157. # match u8 0x85 0xff at 48 \
  158. # match u8 0x3a 0xff at 14 \
  159. # flowid 1:14
  160. ## 134
  161. #$TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 610 u32 \
  162. # match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  163. # match u8 0x86 0xff at 48 \
  164. # match u8 0x3a 0xff at 14 \
  165. # flowid 1:14
  166. ## 135
  167. #$TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 610 u32 \
  168. # match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  169. # match u8 0x87 0xff at 48 \
  170. # match u8 0x3a 0xff at 14 \
  171. # flowid 1:14
  172. ## 136
  173. #$TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 610 u32 \
  174. # match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  175. # match u8 0x88 0xff at 48 \
  176. # match u8 0x3a 0xff at 14 \
  177. # flowid 1:14
  178. ## 137
  179. #$TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 610 u32 \
  180. # match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  181. # match u8 0x89 0xff at 48 \
  182. # match u8 0x3a 0xff at 14 \
  183. # flowid 1:14
  184. # ICMPv6 echo request
  185. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 610 u32 \
  186. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  187. match u8 0x3a 0xff at 14 \
  188. match u8 0x80 0xff at 48 \
  189. flowid 1:13
  190. # ICMPv6 echo reply
  191. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 610 u32 \
  192. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  193. match u8 0x3a 0xff at 14 \
  194. match u8 0x81 0xff at 48 \
  195. flowid 1:13
  196. #IPV6
  197. #BE: careful, will override ICMP
  198. #$TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 600 u32 \
  199. # match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  200. # match u16 0x0000 0x0fb0 at 8 \
  201. # flowid 1:12
  202. #AF42
  203. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 601 u32 \
  204. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  205. match u16 0x0900 0x0fc0 at 8 \
  206. flowid 1:11
  207. #EF
  208. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 602 u32 \
  209. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  210. match u16 0x0b80 0x0fc0 at 8 \
  211. flowid 1:11
  212. #CS1
  213. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 603 u32 \
  214. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  215. match u16 0x0200 0x0fc0 at 8 \
  216. flowid 1:13
  217. #IMM
  218. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 604 u32 \
  219. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  220. match u16 0x0100 0x0fc0 at 8 \
  221. flowid 1:11
  222. #CS3
  223. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 605 u32 \
  224. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  225. match u16 0x0c00 0x0fc0 at 8 \
  226. flowid 1:11
  227. #CS6
  228. $TC filter add dev $IFACE parent 1:0 protocol ${PPPOE_SESSION_ETHERTYPE} prio 606 u32 \
  229. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  230. match u16 0x0e00 0x0fc0 at 8 \
  231. flowid 1:11
  232. #diffserv $IFACE
  233. }
  234. ingress() {
  235. CEIL=$DOWNLINK
  236. PRIO_RATE=`expr $CEIL / 3` # Ceiling for prioirty
  237. BE_RATE=`expr $CEIL / 6` # Min for best effort
  238. BK_RATE=`expr $CEIL / 6` # Min for background
  239. BE_CEIL=`expr $CEIL - 16` # A little slop at the top
  240. LQ="quantum `get_mtu $IFACE $CEIL`"
  241. $TC qdisc del dev $IFACE handle ffff: ingress 2> /dev/null
  242. $TC qdisc add dev $IFACE handle ffff: ingress
  243. $TC qdisc del dev $DEV root 2> /dev/null
  244. if [ "$SQUASH_INGRESS" = "1" ]
  245. then
  246. sqm_logger "Do not perform DSCP based filtering on ingress. (1-tier classification)"
  247. # Revert to no dscp based filtering
  248. $TC qdisc del dev $DEV root 2>/dev/null
  249. $TC qdisc add dev $DEV root handle 1: `get_stab_string` htb default 10
  250. $TC class add dev $DEV parent 1: classid 1:1 htb $LQ rate ${DOWNLINK}kbit ceil ${DOWNLINK}kbit `get_htb_adsll_string`
  251. $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`
  252. $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}
  253. else
  254. sqm_logger "Perform DSCP based filtering on ingress. (3-tier classification)"
  255. $TC qdisc add dev $DEV root handle 1: `get_stab_string` htb default 12
  256. $TC class add dev $DEV parent 1: classid 1:1 htb $LQ rate ${CEIL}kbit ceil ${CEIL}kbit `get_htb_adsll_string`
  257. $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`
  258. $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`
  259. $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`
  260. $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`
  261. # I'd prefer to use a pre-nat filter but that causes permutation...
  262. $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}
  263. $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}
  264. $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}
  265. #sm: for PPPoE packet testing
  266. $TC class add dev $DEV parent 1:1 classid 1:14 htb $LQ rate ${BK_RATE}kbit ceil ${BE_CEIL}kbit prio 3 `get_htb_adsll_string`
  267. $TC qdisc add dev $DEV parent 1:14 handle 140: $QDISC `get_limit ${ILIMIT}` `get_target "${ITARGET}" ${DOWNLINK}` `get_ecn ${IECN}` `get_quantum 300` `get_flows ${BK_RATE}` ${IQDISC_OPTS}
  268. #diffserv $DEV
  269. diffserv_pppoe $DEV
  270. fi
  271. ifconfig $DEV up
  272. # redirect all IP packets arriving in $IFACE to ifb0
  273. $TC filter add dev $IFACE parent ffff: protocol all prio 10 u32 \
  274. match u32 0 0 flowid 1:1 action mirred egress redirect dev $DEV
  275. }
  276. do_modules
  277. ipt_setup
  278. if [ "$UPLINK" -ne 0 ];
  279. then
  280. egress
  281. sqm_logger "egress shaping activated"
  282. else
  283. sqm_logger "egress shaping deactivated"
  284. tc qdisc del dev $IFACE root 2> /dev/null
  285. fi
  286. if [ "$DOWNLINK" -ne 0 ];
  287. then
  288. ingress
  289. sqm_logger "ingress shaping activated"
  290. else
  291. sqm_logger "ingress shaping deactivated"
  292. tc qdisc del dev $DEV root 2> /dev/null
  293. tc qdisc del dev $IFACE ingress 2> /dev/null
  294. fi
  295. # References:
  296. # This alternate shaper attempts to go for 1/u performance in a clever way
  297. # http://git.coverfire.com/?p=linux-qos-scripts.git;a=blob;f=src-3tos.sh;hb=HEAD
  298. # Comments
  299. # This does the right thing with ipv6 traffic.
  300. # It also tries to leverage diffserv to some sane extent. In particular,
  301. # the 'priority' queue is limited to 33% of the total, so EF, and IMM traffic
  302. # cannot starve other types. The rfc suggested 30%. 30% is probably
  303. # a lot in today's world.
  304. # Flaws
  305. # Many!