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.

458 lines
13 KiB

  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License version 2 as
  3. # published by the Free Software Foundation.
  4. #
  5. # Copyright (C) 2012-4 Michael D. Taht, Toke Høiland-Jørgensen, Sebastian Moeller
  6. #improve the logread output
  7. sqm_logger() {
  8. logger -t SQM -s ${1}
  9. }
  10. insmod() {
  11. lsmod | grep -q ^$1 || $INSMOD $1
  12. }
  13. ipt() {
  14. d=`echo $* | sed s/-A/-D/g`
  15. [ "$d" != "$*" ] && {
  16. iptables $d > /dev/null 2>&1
  17. ip6tables $d > /dev/null 2>&1
  18. }
  19. d=`echo $* | sed s/-I/-D/g`
  20. [ "$d" != "$*" ] && {
  21. iptables $d > /dev/null 2>&1
  22. ip6tables $d > /dev/null 2>&1
  23. }
  24. iptables $* > /dev/null 2>&1
  25. ip6tables $* > /dev/null 2>&1
  26. }
  27. do_modules() {
  28. #sm TODO: check first whether the modules exist and only load then
  29. insmod act_ipt
  30. insmod sch_$QDISC
  31. insmod sch_ingress
  32. insmod act_mirred
  33. insmod cls_fw
  34. insmod sch_htb
  35. }
  36. # You need to jiggle these parameters. Note limits are tuned towards a <10Mbit uplink <60Mbup down
  37. [ -z "$UPLINK" ] && UPLINK=2302
  38. [ -z "$DOWNLINK" ] && DOWNLINK=14698
  39. [ -z "$IFACE" ] && IFACE=ge00
  40. [ -z "$QDISC" ] && QDISC=fq_codel
  41. [ -z "$LLAM" ] && LLAM="tc_stab"
  42. [ -z "$LINKLAYER" ] && LINKLAYER="none"
  43. [ -z "$OVERHEAD" ] && OVERHEAD=0
  44. [ -z "$STAB_MTU" ] && STAB_MTU=2047
  45. [ -z "$STAB_MPU" ] && STAB_MPU=0
  46. [ -z "$STAB_TSIZE" ] && STAB_TSIZE=512
  47. [ -z "$AUTOFLOW" ] && AUTOFLOW=0
  48. [ -z "$LIMIT" ] && LIMIT=1001 # sane global default for *LIMIT for fq_codel on a small memory device
  49. [ -z "$ILIMIT" ] && ILIMIT=
  50. [ -z "$ELIMIT" ] && ELIMIT=
  51. [ -z "$ITARGET" ] && ITARGET=
  52. [ -z "$ETARGET" ] && ETARGET=
  53. [ -z "$IECN" ] && IECN="ECN"
  54. [ -z "$EECN" ] && EECN="NOECN"
  55. [ -z "$SQUASH_DSCP" ] && SQUASH_DSCP="1"
  56. [ -z "$SQUASH_INGRESS" ] && SQUASH_INGRESS="1"
  57. [ -z "$IQDISC_OPTS" ] && IQDISC_OPTS=""
  58. [ -z "$EQDISC_OPTS" ] && EQDISC_OPTS=""
  59. [ -z "$TC" ] && TC=`which tc`
  60. #[ -z "$TC" ] && TC="sqm_logger tc"# this redirects all tc calls into the log
  61. [ -z "$IP" ] && IP=$( which ip )
  62. [ -z "$INSMOD" ] && INSMOD=`which insmod`
  63. [ -z "$TARGET" ] && TARGET="5ms"
  64. [ -z "$IPT_MASK" ] && IPT_MASK="0xff"
  65. [ -z "$IPT_MASK_STRING" ] && IPT_MASK_STRING="/${IPT_MASK}" # for set-mark
  66. #sqm_logger "${0} IPT_MASK: ${IPT_MASK_STRING}"
  67. # find the ifb device associated with a specific interface, return nothing of no ifb is associated with IF
  68. get_ifb_associated_with_if() {
  69. CUR_IF=$1
  70. # CUR_IFB=$( tc -p filter show parent ffff: dev ${CUR_IF} | grep -o -e ifb'[[:digit:]]\+' )
  71. CUR_IFB=$( tc -p filter show parent ffff: dev ${CUR_IF} | grep -o -e ifb'[^)]\+' ) # my editor's syntax coloration is limitied so I need a single quote in this line (between eiditor and s)
  72. sqm_logger "ifb associated with interface ${CUR_IF}: ${CUR_IFB}"
  73. echo ${CUR_IFB}
  74. }
  75. # ATTENTION, IFB names can only be 15 chararcters, so we chop of excessive characters at the start of the interface name
  76. # if required
  77. create_new_ifb_for_if() {
  78. CUR_IF=$1
  79. MAX_IF_NAME_LENGTH=15
  80. IFB_PREFIX="ifb4"
  81. NEW_IFB="${IFB_PREFIX}${CUR_IF}"
  82. IFB_NAME_LENGTH=${#NEW_IFB}
  83. if [ ${IFB_NAME_LENGTH} -gt ${MAX_IF_NAME_LENGTH} ];
  84. then
  85. sqm_logger "The requsted IFB name ${NEW_IFB} is longer than the allowed 15 characters, trying to make it shorter"
  86. OVERLIMIT=$(( ${#NEW_IFB} - ${MAX_IF_NAME_LENGTH} ))
  87. NEW_IFB=${IFB_PREFIX}${CUR_IF:${OVERLIMIT}:$(( ${MAX_IF_NAME_LENGTH} - ${#IFB_PREFIX} ))}
  88. fi
  89. sqm_logger "trying to create new IFB: ${NEW_IFB}"
  90. $IP link add name ${NEW_IFB} type ifb #>/dev/null 2>&1 # better be verbose
  91. echo ${NEW_IFB}
  92. }
  93. # the best match is either the IFB already associated with the current interface or a new named IFB
  94. get_ifb_for_if() {
  95. CUR_IF=$1
  96. # if an ifb is already associated return that
  97. CUR_IFB=$( get_ifb_associated_with_if ${CUR_IF} )
  98. [ -z "$CUR_IFB" ] && CUR_IFB=$( create_new_ifb_for_if ${CUR_IF} )
  99. [ -z "$CUR_IFB" ] && sqm_logger "Could not find existing IFB for ${CUR_IF}, nor create a new IFB instead..."
  100. echo ${CUR_IFB}
  101. }
  102. #sm: we need the functions above before trying to set the ingress IFB device
  103. [ -z "$DEV" ] && DEV=$( get_ifb_for_if ${IFACE} ) # automagically get the right IFB device for the IFACE"
  104. get_htb_adsll_string() {
  105. ADSLL=""
  106. if [ "$LLAM" = "htb_private" -a "$LINKLAYER" != "none" ];
  107. then
  108. # HTB defaults to MTU 1600 and an implicit fixed TSIZE of 256, but HTB as of around 3.10.0
  109. # does not actually use a table in the kernel
  110. ADSLL="mpu ${STAB_MPU} linklayer ${LINKLAYER} overhead ${OVERHEAD} mtu ${STAB_MTU}"
  111. sqm_logger "ADSLL: ${ADSLL}"
  112. fi
  113. echo ${ADSLL}
  114. }
  115. get_stab_string() {
  116. STABSTRING=""
  117. if [ "${LLAM}" = "tc_stab" -a "$LINKLAYER" != "none" ];
  118. then
  119. STABSTRING="stab mtu ${STAB_MTU} tsize ${STAB_TSIZE} mpu ${STAB_MPU} overhead ${OVERHEAD} linklayer ${LINKLAYER}"
  120. sqm_logger "STAB: ${STABSTRING}"
  121. fi
  122. echo ${STABSTRING}
  123. }
  124. sqm_stop() {
  125. $TC qdisc del dev $IFACE ingress
  126. $TC qdisc del dev $IFACE root
  127. $TC qdisc del dev $DEV root
  128. }
  129. # Note this has side effects on the prio variable
  130. # and depends on the interface global too
  131. fc() {
  132. $TC filter add dev $interface protocol ip parent $1 prio $prio u32 match ip tos $2 0xfc classid $3
  133. prio=$(($prio + 1))
  134. $TC filter add dev $interface protocol ipv6 parent $1 prio $prio u32 match ip6 priority $2 0xfc classid $3
  135. prio=$(($prio + 1))
  136. }
  137. fc_pppoe() {
  138. PPPOE_SESSION_ETHERTYPE="0x8864"
  139. PPPOE_DISCOVERY_ETHERTYPE="0x8863"
  140. PPP_PROTO_IP4="0x0021"
  141. PPP_PROTO_IP6="0x0057"
  142. ARP_PROTO_IP4="0x0806"
  143. $TC filter add dev $interface protocol ip parent $1 prio $prio u32 match ip tos $2 0xfc classid $3
  144. $TC filter add dev $interface parent $1 protocol ${PPPOE_SESSION_ETHERTYPE} prio $(( 400 + ${prio} )) u32 \
  145. match u16 ${PPP_PROTO_IP4} 0xffff at 6 \
  146. match u8 $2 0xfc at 9 \
  147. flowid $3
  148. prio=$(($prio + 1))
  149. $TC filter add dev $interface protocol ipv6 parent $1 prio $prio u32 match ip6 priority $2 0xfc classid $3
  150. $TC filter add dev $interface parent $1 protocol ${PPPOE_SESSION_ETHERTYPE} prio $(( 600 + ${prio} )) u32 \
  151. match u16 ${PPP_PROTO_IP6} 0xffff at 6 \
  152. match u16 0x0${2:2:2}0 0x0fc0 at 8 \
  153. flowid $3
  154. prio=$(($prio + 1))
  155. }
  156. # FIXME: actually you need to get the underlying MTU on PPOE thing
  157. get_mtu() {
  158. BW=$2
  159. F=`cat /sys/class/net/$1/mtu`
  160. if [ -z "$F" ]
  161. then
  162. F=1500
  163. fi
  164. if [ $BW -gt 20000 ]
  165. then
  166. F=$(($F * 2))
  167. fi
  168. if [ $BW -gt 30000 ]
  169. then
  170. F=$(($F * 2))
  171. fi
  172. if [ $BW -gt 40000 ]
  173. then
  174. F=$(($F * 2))
  175. fi
  176. if [ $BW -gt 50000 ]
  177. then
  178. F=$(($F * 2))
  179. fi
  180. if [ $BW -gt 60000 ]
  181. then
  182. F=$(($F * 2))
  183. fi
  184. if [ $BW -gt 80000 ]
  185. then
  186. F=$(($F * 2))
  187. fi
  188. echo $F
  189. }
  190. # FIXME should also calculate the limit
  191. # Frankly I think Xfq_codel can pretty much always run with high numbers of flows
  192. # now that it does fate sharing
  193. # But right now I'm trying to match the ns2 model behavior better
  194. # So SET the autoflow variable to 1 if you want the cablelabs behavior
  195. get_flows() {
  196. if [ "$AUTOFLOW" -eq "1" ]
  197. then
  198. FLOWS=8
  199. [ $1 -gt 999 ] && FLOWS=16
  200. [ $1 -gt 2999 ] && FLOWS=32
  201. [ $1 -gt 7999 ] && FLOWS=48
  202. [ $1 -gt 9999 ] && FLOWS=64
  203. [ $1 -gt 19999 ] && FLOWS=128
  204. [ $1 -gt 39999 ] && FLOWS=256
  205. [ $1 -gt 69999 ] && FLOWS=512
  206. [ $1 -gt 99999 ] && FLOWS=1024
  207. case $QDISC in
  208. codel|ns2_codel|pie|*fifo|pfifo_fast) ;;
  209. fq_codel|*fq_codel|sfq) echo flows $FLOWS ;;
  210. esac
  211. fi
  212. }
  213. # set the target parameter, also try to only take well formed inputs
  214. # Note, the link bandwidth in the current direction (ingress or egress)
  215. # is required to adjust the target for slow links
  216. get_target() {
  217. local CUR_TARGET=${1}
  218. local CUR_LINK_KBPS=${2}
  219. [ ! -z "$CUR_TARGET" ] && sqm_logger "cur_target: ${CUR_TARGET} cur_bandwidth: ${CUR_LINK_KBPS}"
  220. CUR_TARGET_STRING=
  221. # either e.g. 100ms or auto
  222. CUR_TARGET_VALUE=$( echo ${CUR_TARGET} | grep -o -e \^'[[:digit:]]\+' )
  223. CUR_TARGET_UNIT=$( echo ${CUR_TARGET} | grep -o -e '[[:alpha:]]\+'\$ )
  224. # [ ! -z "$CUR_TARGET" ] && sqm_logger "CUR_TARGET_VALUE: $CUR_TARGET_VALUE"
  225. # [ ! -z "$CUR_TARGET" ] && sqm_logger "CUR_TARGET_UNIT: $CUR_TARGET_UNIT"
  226. AUTO_TARGET=
  227. UNIT_VALID=
  228. case $QDISC in
  229. *codel|*pie)
  230. if [ ! -z "${CUR_TARGET_VALUE}" -a ! -z "${CUR_TARGET_UNIT}" ];
  231. then
  232. case ${CUR_TARGET_UNIT} in
  233. # permissible units taken from: tc_util.c get_time()
  234. s|sec|secs|ms|msec|msecs|us|usec|usecs)
  235. CUR_TARGET_STRING="target ${CUR_TARGET_VALUE}${CUR_TARGET_UNIT}"
  236. UNIT_VALID="1"
  237. ;;
  238. esac
  239. fi
  240. case ${CUR_TARGET_UNIT} in
  241. auto|Auto|AUTO)
  242. if [ ! -z "${CUR_LINK_KBPS}" ];
  243. then
  244. TMP_TARGET_US=$( adapt_target_to_slow_link $CUR_LINK_KBPS )
  245. TMP_INTERVAL_STRING=$( adapt_interval_to_slow_link $TMP_TARGET_US )
  246. CUR_TARGET_STRING="target ${TMP_TARGET_US}us ${TMP_INTERVAL_STRING}"
  247. AUTO_TARGET="1"
  248. else
  249. sqm_logger "required link bandwidth in kbps not passed to get_target()."
  250. fi
  251. ;;
  252. esac
  253. if [ ! -z "${CUR_TARGET}" ];
  254. then
  255. if [ -z "${CUR_TARGET_VALUE}" -o -z "${UNIT_VALID}" ];
  256. then
  257. [ -z "$AUTO_TARGET" ] && sqm_logger "${CUR_TARGET} is not a well formed tc target specifier; e.g.: 5ms (or s, us), or the string auto."
  258. fi
  259. fi
  260. ;;
  261. esac
  262. # sqm_logger "target: ${CUR_TARGET_STRING}"
  263. echo $CUR_TARGET_STRING
  264. }
  265. # for low bandwidth links fq_codels default target of 5ms does not work too well
  266. # so increase target for slow links (note below roughly 2500kbps a single packet will \
  267. # take more than 5 ms to be tansfered over the wire)
  268. adapt_target_to_slow_link() {
  269. CUR_LINK_KBPS=$1
  270. CUR_EXTENDED_TARGET_US=
  271. MAX_PAKET_DELAY_IN_US_AT_1KBPS=$(( 1000 * 1000 *1540 * 8 / 1000 ))
  272. CUR_EXTENDED_TARGET_US=$(( ${MAX_PAKET_DELAY_IN_US_AT_1KBPS} / ${CUR_LINK_KBPS} )) # note this truncates the decimals
  273. # do not change anything for fast links
  274. [ "$CUR_EXTENDED_TARGET_US" -lt 5000 ] && CUR_EXTENDED_TARGET_US=5000
  275. case ${QDISC} in
  276. *codel|pie)
  277. echo "${CUR_EXTENDED_TARGET_US}"
  278. ;;
  279. esac
  280. }
  281. # codel looks at a whole interval to figure out wether observed latency stayed below target
  282. # if target >= interval that will not work well, so increase interval by the same amonut that target got increased
  283. adapt_interval_to_slow_link() {
  284. CUR_TARGET_US=$1
  285. case ${QDISC} in
  286. *codel)
  287. CUR_EXTENDED_INTERVAL_US=$(( (100 - 5) * 1000 + ${CUR_TARGET_US} ))
  288. echo "interval ${CUR_EXTENDED_INTERVAL_US}us"
  289. ;;
  290. pie)
  291. ## not sure if pie needs this, probably not
  292. #CUR_EXTENDED_TUPDATE_US=$(( (30 - 20) * 1000 + ${CUR_TARGET_US} ))
  293. #echo "tupdate ${CUR_EXTENDED_TUPDATE_US}us"
  294. ;;
  295. esac
  296. }
  297. # set quantum parameter if available for this qdisc
  298. get_quantum() {
  299. case $QDISC in
  300. *fq_codel|fq_pie|drr) echo quantum $1 ;;
  301. *) ;;
  302. esac
  303. }
  304. # only show limits to qdiscs that can handle them...
  305. # Note that $LIMIT contains the default limit
  306. get_limit() {
  307. CURLIMIT=$1
  308. case $QDISC in
  309. *codel|*pie|pfifo_fast|sfq|pfifo) [ -z ${CURLIMIT} ] && CURLIMIT=${LIMIT} # use the global default limit
  310. ;;
  311. bfifo) [ -z "$CURLIMIT" ] && [ ! -z "$LIMIT" ] && CURLIMIT=$(( ${LIMIT} * $( cat /sys/class/net/${IFACE}/mtu ) )) # bfifo defaults to txquelength * MTU,
  312. ;;
  313. *) sqm_logger "${QDISC} does not support a limit"
  314. ;;
  315. esac
  316. sqm_logger "get_limit: $1 CURLIMIT: ${CURLIMIT}"
  317. if [ ! -z "$CURLIMIT" ]
  318. then
  319. echo "limit ${CURLIMIT}"
  320. fi
  321. }
  322. get_ecn() {
  323. CURECN=$1
  324. #sqm_logger CURECN: $CURECN
  325. case ${CURECN} in
  326. ECN)
  327. case $QDISC in
  328. *codel|*pie|*red)
  329. CURECN=ecn
  330. ;;
  331. *)
  332. CURECN=""
  333. ;;
  334. esac
  335. ;;
  336. NOECN)
  337. case $QDISC in
  338. *codel|*pie|*red)
  339. CURECN=noecn
  340. ;;
  341. *)
  342. CURECN=""
  343. ;;
  344. esac
  345. ;;
  346. *)
  347. sqm_logger "ecn value $1 not handled"
  348. ;;
  349. esac
  350. #sqm_logger "get_ECN: $1 CURECN: ${CURECN} IECN: ${IECN} EECN: ${EECN}"
  351. echo ${CURECN}
  352. }
  353. # This could be a complete diffserv implementation
  354. diffserv() {
  355. interface=$1
  356. prio=1
  357. # Catchall
  358. $TC filter add dev $interface parent 1:0 protocol all prio 999 u32 \
  359. match ip protocol 0 0x00 flowid 1:12
  360. # Find the most common matches fast
  361. #fc_pppoe() instead of fc() with effectice ingress classification for pppoe is very expensive and destroys LUL
  362. fc 1:0 0x00 1:12 # BE
  363. fc 1:0 0x20 1:13 # CS1
  364. fc 1:0 0x10 1:11 # IMM
  365. fc 1:0 0xb8 1:11 # EF
  366. fc 1:0 0xc0 1:11 # CS3
  367. fc 1:0 0xe0 1:11 # CS6
  368. fc 1:0 0x90 1:11 # AF42 (mosh)
  369. # Arp traffic
  370. $TC filter add dev $interface protocol arp parent 1:0 prio $prio handle 500 fw flowid 1:11
  371. prio=$(($prio + 1))
  372. }
  373. diffserv_pppoe() {
  374. interface=$1
  375. prio=1
  376. # Catchall
  377. $TC filter add dev $interface parent 1:0 protocol all prio 999 u32 \
  378. match ip protocol 0 0x00 flowid 1:12
  379. # Find the most common matches fast
  380. #fc_pppoe() instead of fc() with effectice ingress classification for pppoe is very expensive and destroys LUL
  381. fc_pppoe 1:0 0x00 1:12 # BE
  382. fc_pppoe 1:0 0x20 1:13 # CS1
  383. fc_pppoe 1:0 0x10 1:11 # IMM
  384. fc_pppoe 1:0 0xb8 1:11 # EF
  385. fc_pppoe 1:0 0xc0 1:11 # CS3
  386. fc_pppoe 1:0 0xe0 1:11 # CS6
  387. fc_pppoe 1:0 0x90 1:11 # AF42 (mosh)
  388. # Arp traffic
  389. $TC filter add dev $interface protocol arp parent 1:0 prio $prio handle 500 fw flowid 1:11
  390. prio=$(($prio + 1))
  391. }