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.

486 lines
14 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. # empty field in GUI or undefined GUI variable now defaults to auto
  241. if [ -z "${CUR_TARGET_VALUE}" -a -z "${CUR_TARGET_UNIT}" ];
  242. then
  243. if [ ! -z "${CUR_LINK_KBPS}" ];
  244. then
  245. TMP_TARGET_US=$( adapt_target_to_slow_link $CUR_LINK_KBPS )
  246. TMP_INTERVAL_STRING=$( adapt_interval_to_slow_link $TMP_TARGET_US )
  247. CUR_TARGET_STRING="target ${TMP_TARGET_US}us ${TMP_INTERVAL_STRING}"
  248. AUTO_TARGET="1"
  249. sqm_logger "get_target defaulting to auto."
  250. else
  251. sqm_logger "required link bandwidth in kbps not passed to get_target()."
  252. fi
  253. fi
  254. # but still allow explicit use of the keyword auto for backward compatibility
  255. case ${CUR_TARGET_UNIT} in
  256. auto|Auto|AUTO)
  257. if [ ! -z "${CUR_LINK_KBPS}" ];
  258. then
  259. TMP_TARGET_US=$( adapt_target_to_slow_link $CUR_LINK_KBPS )
  260. TMP_INTERVAL_STRING=$( adapt_interval_to_slow_link $TMP_TARGET_US )
  261. CUR_TARGET_STRING="target ${TMP_TARGET_US}us ${TMP_INTERVAL_STRING}"
  262. AUTO_TARGET="1"
  263. else
  264. sqm_logger "required link bandwidth in kbps not passed to get_target()."
  265. fi
  266. ;;
  267. esac
  268. case ${CUR_TARGET_UNIT} in
  269. default|Default|DEFAULT)
  270. if [ ! -z "${CUR_LINK_KBPS}" ];
  271. then
  272. CUR_TARGET_STRING="" # return nothing so the default target is not over-ridden...
  273. AUTO_TARGET="1"
  274. #sqm_logger "get_target using qdisc default, no explicit target string passed."
  275. else
  276. sqm_logger "required link bandwidth in kbps not passed to get_target()."
  277. fi
  278. ;;
  279. esac
  280. if [ ! -z "${CUR_TARGET}" ];
  281. then
  282. if [ -z "${CUR_TARGET_VALUE}" -o -z "${UNIT_VALID}" ];
  283. then
  284. [ -z "$AUTO_TARGET" ] && sqm_logger "${CUR_TARGET} is not a well formed tc target specifier; e.g.: 5ms (or s, us), or one of the strings auto or default."
  285. fi
  286. fi
  287. ;;
  288. esac
  289. # sqm_logger "target: ${CUR_TARGET_STRING}"
  290. echo $CUR_TARGET_STRING
  291. }
  292. # for low bandwidth links fq_codels default target of 5ms does not work too well
  293. # so increase target for slow links (note below roughly 2500kbps a single packet will \
  294. # take more than 5 ms to be tansfered over the wire)
  295. adapt_target_to_slow_link() {
  296. CUR_LINK_KBPS=$1
  297. CUR_EXTENDED_TARGET_US=
  298. MAX_PAKET_DELAY_IN_US_AT_1KBPS=$(( 1000 * 1000 *1540 * 8 / 1000 ))
  299. CUR_EXTENDED_TARGET_US=$(( ${MAX_PAKET_DELAY_IN_US_AT_1KBPS} / ${CUR_LINK_KBPS} )) # note this truncates the decimals
  300. # do not change anything for fast links
  301. [ "$CUR_EXTENDED_TARGET_US" -lt 5000 ] && CUR_EXTENDED_TARGET_US=5000
  302. case ${QDISC} in
  303. *codel|pie)
  304. echo "${CUR_EXTENDED_TARGET_US}"
  305. ;;
  306. esac
  307. }
  308. # codel looks at a whole interval to figure out wether observed latency stayed below target
  309. # if target >= interval that will not work well, so increase interval by the same amonut that target got increased
  310. adapt_interval_to_slow_link() {
  311. CUR_TARGET_US=$1
  312. case ${QDISC} in
  313. *codel)
  314. CUR_EXTENDED_INTERVAL_US=$(( (100 - 5) * 1000 + ${CUR_TARGET_US} ))
  315. echo "interval ${CUR_EXTENDED_INTERVAL_US}us"
  316. ;;
  317. pie)
  318. ## not sure if pie needs this, probably not
  319. #CUR_EXTENDED_TUPDATE_US=$(( (30 - 20) * 1000 + ${CUR_TARGET_US} ))
  320. #echo "tupdate ${CUR_EXTENDED_TUPDATE_US}us"
  321. ;;
  322. esac
  323. }
  324. # set quantum parameter if available for this qdisc
  325. get_quantum() {
  326. case $QDISC in
  327. *fq_codel|fq_pie|drr) echo quantum $1 ;;
  328. *) ;;
  329. esac
  330. }
  331. # only show limits to qdiscs that can handle them...
  332. # Note that $LIMIT contains the default limit
  333. get_limit() {
  334. CURLIMIT=$1
  335. case $QDISC in
  336. *codel|*pie|pfifo_fast|sfq|pfifo) [ -z ${CURLIMIT} ] && CURLIMIT=${LIMIT} # use the global default limit
  337. ;;
  338. bfifo) [ -z "$CURLIMIT" ] && [ ! -z "$LIMIT" ] && CURLIMIT=$(( ${LIMIT} * $( cat /sys/class/net/${IFACE}/mtu ) )) # bfifo defaults to txquelength * MTU,
  339. ;;
  340. *) sqm_logger "${QDISC} does not support a limit"
  341. ;;
  342. esac
  343. sqm_logger "get_limit: $1 CURLIMIT: ${CURLIMIT}"
  344. if [ ! -z "$CURLIMIT" ]
  345. then
  346. echo "limit ${CURLIMIT}"
  347. fi
  348. }
  349. get_ecn() {
  350. CURECN=$1
  351. #sqm_logger CURECN: $CURECN
  352. case ${CURECN} in
  353. ECN)
  354. case $QDISC in
  355. *codel|*pie|*red)
  356. CURECN=ecn
  357. ;;
  358. *)
  359. CURECN=""
  360. ;;
  361. esac
  362. ;;
  363. NOECN)
  364. case $QDISC in
  365. *codel|*pie|*red)
  366. CURECN=noecn
  367. ;;
  368. *)
  369. CURECN=""
  370. ;;
  371. esac
  372. ;;
  373. *)
  374. sqm_logger "ecn value $1 not handled"
  375. ;;
  376. esac
  377. #sqm_logger "get_ECN: $1 CURECN: ${CURECN} IECN: ${IECN} EECN: ${EECN}"
  378. echo ${CURECN}
  379. }
  380. # This could be a complete diffserv implementation
  381. diffserv() {
  382. interface=$1
  383. prio=1
  384. # Catchall
  385. $TC filter add dev $interface parent 1:0 protocol all prio 999 u32 \
  386. match ip protocol 0 0x00 flowid 1:12
  387. # Find the most common matches fast
  388. #fc_pppoe() instead of fc() with effectice ingress classification for pppoe is very expensive and destroys LUL
  389. fc 1:0 0x00 1:12 # BE
  390. fc 1:0 0x20 1:13 # CS1
  391. fc 1:0 0x10 1:11 # IMM
  392. fc 1:0 0xb8 1:11 # EF
  393. fc 1:0 0xc0 1:11 # CS3
  394. fc 1:0 0xe0 1:11 # CS6
  395. fc 1:0 0x90 1:11 # AF42 (mosh)
  396. # Arp traffic
  397. $TC filter add dev $interface protocol arp parent 1:0 prio $prio handle 500 fw flowid 1:11
  398. prio=$(($prio + 1))
  399. }
  400. diffserv_pppoe() {
  401. interface=$1
  402. prio=1
  403. # Catchall
  404. $TC filter add dev $interface parent 1:0 protocol all prio 999 u32 \
  405. match ip protocol 0 0x00 flowid 1:12
  406. # Find the most common matches fast
  407. #fc_pppoe() instead of fc() with effectice ingress classification for pppoe is very expensive and destroys LUL
  408. fc_pppoe 1:0 0x00 1:12 # BE
  409. fc_pppoe 1:0 0x20 1:13 # CS1
  410. fc_pppoe 1:0 0x10 1:11 # IMM
  411. fc_pppoe 1:0 0xb8 1:11 # EF
  412. fc_pppoe 1:0 0xc0 1:11 # CS3
  413. fc_pppoe 1:0 0xe0 1:11 # CS6
  414. fc_pppoe 1:0 0x90 1:11 # AF42 (mosh)
  415. # Arp traffic
  416. $TC filter add dev $interface protocol arp parent 1:0 prio $prio handle 500 fw flowid 1:11
  417. prio=$(($prio + 1))
  418. }