| 1 | #!/bin/sh |
|---|
| 2 | # |
|---|
| 3 | # Wonder Shaper r24 |
|---|
| 4 | # |
|---|
| 5 | |
|---|
| 6 | # --# |
|---|
| 7 | # Some small things changed by Thomas Lundquist <thomasez@zelow.no> |
|---|
| 8 | # to make it work with floppyfw |
|---|
| 9 | # Basically, it's grabbing values from files in /etc and changing the |
|---|
| 10 | # calculations from bash'ism to using expr. |
|---|
| 11 | # r24 060815 |
|---|
| 12 | # Brad Skiff added mods for VoIP class <bbbrad2dbone@hotmail.com> |
|---|
| 13 | # adapted portions from Kristian Kielhofner's htb script <kris@krisk.org> |
|---|
| 14 | # changed r2q 100 to r2q calc (r2q = 125*${UPLINK}kbit/mtu) |
|---|
| 15 | # changed default class to 30 from 20, added "...Stopped - exit" message. |
|---|
| 16 | # added config RT10 rate value and calc'd all other rate + ceil values |
|---|
| 17 | # upped ingress burst from 10K to 20K - boosted download speed |
|---|
| 18 | # removed 90% and 80% CUTTED values and added calc'd quantum to classes |
|---|
| 19 | # added kog script suggestions correctly and for-do loop for stochastic fairness |
|---|
| 20 | # thomasez: s/SERVER_IP/PHONE_IP/ |
|---|
| 21 | # added vars to config for flexibility plus count function for LO/HI_RTPPORT |
|---|
| 22 | # added logic to check for empty ports vars in config for flexibility |
|---|
| 23 | # put SIP ports in class 10 since some (asterisk) use them for RTP too. |
|---|
| 24 | # added logic to remove VoIP class if PHONE_IP= .or. RT10= blank in config. |
|---|
| 25 | # added WARNING message if no voip ports are entered in config. |
|---|
| 26 | # commented out ICMP and made it a test template. |
|---|
| 27 | # removed class 40 and re-distributed its percentage to class 20 and 30 |
|---|
| 28 | # added INGRESS_COP var and "nocop" arg for policer on/off sw |
|---|
| 29 | # -- |
|---|
| 30 | |
|---|
| 31 | . /etc/functions.inc |
|---|
| 32 | . /etc/config |
|---|
| 33 | . /etc/outside.info |
|---|
| 34 | . /etc/inside.info |
|---|
| 35 | |
|---|
| 36 | # |
|---|
| 37 | # calculated values |
|---|
| 38 | # |
|---|
| 39 | cut_to_percentage () { |
|---|
| 40 | echo `expr $2 \* $1 / 100` |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | calc_r2q_val () { |
|---|
| 44 | echo `expr $2 \* $1 / 1500` |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | calc_qtc_val () { |
|---|
| 48 | echo `expr $2 / $1` |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | calc_prod_vals () { |
|---|
| 52 | echo `expr $2 \* $1` |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | calc_diff_vals () { |
|---|
| 56 | echo `expr $1 - $2` |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | DEV=$OUTSIDE_DEVICE |
|---|
| 60 | |
|---|
| 61 | if [ "$1" = "status" ] |
|---|
| 62 | then |
|---|
| 63 | tc -s qdisc ls dev $DEV |
|---|
| 64 | tc -s class ls dev $DEV |
|---|
| 65 | if [ -n "$(tc -s qdisc |grep ingress)" ] |
|---|
| 66 | then echo "INGRESS_COP=ON - limited DL speed" |
|---|
| 67 | else echo "INGRESS_COP=OFF - maximum DL speed" |
|---|
| 68 | fi |
|---|
| 69 | exit |
|---|
| 70 | fi |
|---|
| 71 | |
|---|
| 72 | # This makes it "Somewhat less" based on the accurate numbers in /etc/config |
|---|
| 73 | # r24 adds nocop argument |
|---|
| 74 | if [ "$1" = "nocop" ] |
|---|
| 75 | then |
|---|
| 76 | INGRESS_COP=n |
|---|
| 77 | DOWNLINK=$DOWNLOAD_SPEED |
|---|
| 78 | else |
|---|
| 79 | INGRESS_COP=y |
|---|
| 80 | DOWNLINK=`cut_to_percentage $DOWNLOAD_SPEED 94` |
|---|
| 81 | fi |
|---|
| 82 | UPLINK=`cut_to_percentage $UPLOAD_SPEED 94` |
|---|
| 83 | |
|---|
| 84 | # Calculate RTXX rates as a % of (UPLINK-RT10 rate in config) in bits/sec |
|---|
| 85 | UPLINKBPS=`calc_prod_vals $UPLINK 1000` |
|---|
| 86 | if [ -n "$PHONE_IP" ] && [ -n "$RT10" ] |
|---|
| 87 | then |
|---|
| 88 | RTUPLBPS=`calc_diff_vals $UPLINKBPS $RT10` |
|---|
| 89 | else |
|---|
| 90 | RTUPLBPS=$UPLINKBPS |
|---|
| 91 | fi |
|---|
| 92 | RT20=`cut_to_percentage $RTUPLBPS 30` |
|---|
| 93 | RT30=`cut_to_percentage $RTUPLBPS 70` |
|---|
| 94 | |
|---|
| 95 | QTUM=`calc_r2q_val ${UPLINK} 125` |
|---|
| 96 | |
|---|
| 97 | if [ "$1" != "stop" ] |
|---|
| 98 | then |
|---|
| 99 | echo "Wondershaper with downlink $DOWNLINK - uplink $UPLINK - r2q $QTUM - on $DEV" |
|---|
| 100 | fi |
|---|
| 101 | |
|---|
| 102 | # clean existing down- and uplink qdiscs, hide errors |
|---|
| 103 | tc qdisc del dev $DEV root 2> /dev/null > /dev/null |
|---|
| 104 | tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null |
|---|
| 105 | |
|---|
| 106 | if [ "$1" = "stop" ] |
|---|
| 107 | then |
|---|
| 108 | echo "Wondershaper Stopped - exit" |
|---|
| 109 | exit |
|---|
| 110 | fi |
|---|
| 111 | |
|---|
| 112 | ###### uplink |
|---|
| 113 | |
|---|
| 114 | # install root HTB, point default traffic to 1:30: |
|---|
| 115 | |
|---|
| 116 | # The "r2q 100" may be wrong but default is 10 and seems to be too little. |
|---|
| 117 | # It should probably be calculated somehow. r2q = 125*${UPLINK}/mtu |
|---|
| 118 | |
|---|
| 119 | QTUM=`calc_r2q_val ${UPLINK} 125` |
|---|
| 120 | tc qdisc add dev $DEV root handle 1: htb default 30 r2q $QTUM |
|---|
| 121 | |
|---|
| 122 | # shape everything at $UPLINK speed - this prevents huge queues in your |
|---|
| 123 | # DSL modem which destroy latency: |
|---|
| 124 | tc class add dev $DEV parent 1: classid 1:1 htb rate ${UPLINK}kbit ceil ${UPLINK}kbit burst 6k |
|---|
| 125 | |
|---|
| 126 | if [ -n "$PHONE_IP" ] && [ -n "$RT10" ] |
|---|
| 127 | then |
|---|
| 128 | QTC10=`calc_qtc_val $QTUM $RT10` |
|---|
| 129 | fi |
|---|
| 130 | QTC20=`calc_qtc_val $QTUM $RT20` |
|---|
| 131 | QTC30=`calc_qtc_val $QTUM $RT30` |
|---|
| 132 | |
|---|
| 133 | if [ -n "$PHONE_IP" ] && [ -n "$RT10" ] |
|---|
| 134 | then |
|---|
| 135 | # VoIP prio class 1:10: |
|---|
| 136 | tc class add dev $DEV parent 1:1 classid 1:10 htb rate $RT10 ceil ${UPLINK}kbit burst 6k prio 1 quantum $QTC10 |
|---|
| 137 | fi |
|---|
| 138 | |
|---|
| 139 | # high prio class 1:20 - gets slightly less traffic and a lower priority: |
|---|
| 140 | tc class add dev $DEV parent 1:1 classid 1:20 htb rate $RT20 ceil ${UPLINK}kbit burst 6k prio 2 quantum $QTC20 |
|---|
| 141 | |
|---|
| 142 | # The default class, 'non-interactive' ie 'bulk' traffic in 1:30 |
|---|
| 143 | tc class add dev $DEV parent 1:1 classid 1:30 htb rate $RT30 ceil ${UPLINK}kbit burst 6k prio 3 quantum $QTC30 |
|---|
| 144 | |
|---|
| 145 | # all get Stochastic Fairness: |
|---|
| 146 | if [ -n "$PHONE_IP" ] && [ -n "$RT10" ] |
|---|
| 147 | then |
|---|
| 148 | tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10 |
|---|
| 149 | fi |
|---|
| 150 | |
|---|
| 151 | for i in 20 30 |
|---|
| 152 | do |
|---|
| 153 | tc qdisc add dev $DEV parent 1:$i handle $i: sfq perturb 10 |
|---|
| 154 | done |
|---|
| 155 | |
|---|
| 156 | if [ -n "$PHONE_IP" ] && [ -n "$RT10" ] |
|---|
| 157 | then |
|---|
| 158 | # Voip TOS in 1:10 |
|---|
| 159 | # matching on all I/O data on RTP and/or SIP ports specified in config |
|---|
| 160 | # |
|---|
| 161 | # RTP ports range as defined in config |
|---|
| 162 | if [ -n "$LO_RTPPORT" ] && [ -n "$HI_RTPPORT" ] |
|---|
| 163 | then |
|---|
| 164 | RTP_RANGE=1 |
|---|
| 165 | for a in `count $LO_RTPPORT $HI_RTPPORT` |
|---|
| 166 | do |
|---|
| 167 | tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 match ip dport $a 0xffff flowid 1:10 |
|---|
| 168 | tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 match ip sport $a 0xffff flowid 1:10 |
|---|
| 169 | done |
|---|
| 170 | else RTP_RANGE=0 |
|---|
| 171 | fi |
|---|
| 172 | |
|---|
| 173 | # SIP ports as defined in config |
|---|
| 174 | if [ -n "$INTPORTS" ] |
|---|
| 175 | then |
|---|
| 176 | SIP_ENTRY=1 |
|---|
| 177 | for a in $INTPORTS |
|---|
| 178 | do |
|---|
| 179 | tc filter add dev $DEV parent 1:0 protocol ip prio 11 u32 match ip dport $a 0xffff flowid 1:10 |
|---|
| 180 | tc filter add dev $DEV parent 1:0 protocol ip prio 11 u32 match ip sport $a 0xffff flowid 1:10 |
|---|
| 181 | done |
|---|
| 182 | else SIP_ENTRY=0 |
|---|
| 183 | fi |
|---|
| 184 | |
|---|
| 185 | # Error Check for RTP/SIP ports entries |
|---|
| 186 | if [ "$RTP_RANGE" -lt 1 ] && [ "$SIP_ENTRY" -lt 1 ] |
|---|
| 187 | then |
|---|
| 188 | echo "WARNING: You MUST enter LO_RTPPORT AND HI_RTPPORT - OR INTPORT" |
|---|
| 189 | echo " - OR ALL Three values in the config to get VoIP priority." |
|---|
| 190 | fi |
|---|
| 191 | fi |
|---|
| 192 | |
|---|
| 193 | # TOS Minimum Delay (ssh, NOT scp) in 1:20: |
|---|
| 194 | tc filter add dev $DEV parent 1:0 protocol ip prio 20 u32 match ip tos 0x10 0xff flowid 1:20 |
|---|
| 195 | |
|---|
| 196 | # DNS in interactive class 1:20 |
|---|
| 197 | tc filter add dev $DEV parent 1:0 protocol ip prio 21 u32 match ip sport 53 0xffff flowid 1:20 |
|---|
| 198 | tc filter add dev $DEV parent 1:0 protocol ip prio 22 u32 match ip dport 53 0xffff flowid 1:20 |
|---|
| 199 | |
|---|
| 200 | # only give TCP ACK's higher priority if this connection is asymmetrical |
|---|
| 201 | if [ ! $DOWNLINK = $UPLINK ] |
|---|
| 202 | then |
|---|
| 203 | # give TCP ACK's higher priority in 1:20 |
|---|
| 204 | tc filter add dev $DEV parent 1: protocol ip prio 23 u32 \ |
|---|
| 205 | match ip protocol 6 0xff \ |
|---|
| 206 | match u8 0x05 0x0f at 0 \ |
|---|
| 207 | match u16 0x0000 0xffc0 at 2 \ |
|---|
| 208 | match u8 0x10 0xff at 33 \ |
|---|
| 209 | flowid 1:20 |
|---|
| 210 | fi |
|---|
| 211 | |
|---|
| 212 | # ICMP Test - Remove '#' below to test class 1:20 ping response time: |
|---|
| 213 | # tc filter add dev $DEV parent 1: protocol ip prio 25 u32 match ip protocol 1 0xff flowid 1:20 |
|---|
| 214 | |
|---|
| 215 | # The default class, 'non-interactive' ie 'bulk' traffic end up here in 1:30 |
|---|
| 216 | tc filter add dev $DEV parent 1: protocol ip prio 30 u32 match ip dst 0.0.0.0/0 flowid 1:30 |
|---|
| 217 | |
|---|
| 218 | # r24 INGRESS_COP on/off logic |
|---|
| 219 | if [ "$INGRESS_COP" = "y" ] |
|---|
| 220 | then |
|---|
| 221 | ########## downlink ############# |
|---|
| 222 | # slow downloads down to somewhat less than the real speed to prevent |
|---|
| 223 | # queuing at our ISP. Tune to see how high you can set it. |
|---|
| 224 | # ISPs tend to have *huge* queues to make sure big downloads are fast |
|---|
| 225 | # |
|---|
| 226 | # attach ingress policer: |
|---|
| 227 | |
|---|
| 228 | tc qdisc add dev $DEV handle ffff: ingress |
|---|
| 229 | |
|---|
| 230 | # filter *everything* to it (0.0.0.0/0), drop everything that's |
|---|
| 231 | # coming in too fast: |
|---|
| 232 | |
|---|
| 233 | tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \ |
|---|
| 234 | 0.0.0.0/0 police rate ${DOWNLINK}kbit burst 20k drop flowid :1 |
|---|
| 235 | fi |
|---|