-3

there i am trying to understand the purpose of this shell script. If anyone can help out i would appreciate it. Thanks! This is located in android system at system/etc/throttle.sh

#!/system/bin/sh
# traffic control; arg1:ifname, arg2: rx, arg 3 tx.
#
export PATH=/data:$PATH

# clear rules
tc qdisc del dev $1 root
tc qdisc del dev $1 ingress
tc qdisc del dev ifb0 root

# set interface throttle
tc qdisc add dev $1 root handle 1: htb default 1 r2q 1000
tc class add dev $1 parent 1: classid 1:1 htb rate $3kbit
ifconfig ifb0 up
tc qdisc add dev ifb0 root handle 1: htb default 1 r2q 1000
tc class add dev ifb0 parent 1: classid 1:1 htb rate $2kbit
tc qdisc add dev $1 ingress
tc filter add dev $1 parent ffff: protocol ip prio 10 u32 match \
    u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0
risto
  • 1
  • 2

1 Answers1

2

That is a traffic shaping (also known a throttling) script.

It appears to take 3 arguments. The interface, the receive limits and the transmit limits.

This first part just deletes everything:

tc qdisc del dev $1 root
tc qdisc del dev $1 ingress
tc qdisc del dev ifb0 root

As you can see the first parameter ($1) is the device interface, for example it could be the wifi interface. It also resets interface ifb0 which is an alternative to tc filters for handling ingress traffic, by redirecting it to a virtual (ifb0) interface and treat is as egress traffic there. The idea is that you can shape egress traffic but not ingress traffic, thereby if you can make all traffic egress then you can shape it.

This line creates a scheduler (qdisc) for the traffic on the interface supplied in paramter $1:

tc qdisc add dev $1 root handle 1: htb default 1 r2q 1000

here you define a class to which a rule is applied (in this case a rate of parameter $3, which is egress in kilobits):

tc class add dev $1 parent 1: classid 1:1 htb rate $3kbit

here you create another scheduler for the virtual interface and a class with a rule defining a rate of parameter $2, which is ingress, in kilobits:

tc qdisc add dev ifb0 root handle 1: htb default 1 r2q 1000
tc class add dev ifb0 parent 1: classid 1:1 htb rate $2kbit

Full info of components of Linux Traffic Control can be found here.

UPDATE: by the way, in a regular linux box you can see the status of the traffic shaping queues by using tc -s. For example you could try to issue the following commands in your phone and they should work too:

tc -s qdisc ls dev ifb0
tc -s class ls dev ifb0

That will display an abundance of information regarding the traffic shaping being performed.

Ricardo
  • 739
  • 5
  • 6
  • What is the line "r2q 1000" representing exactly? – risto Feb 06 '15 at 22:37
  • One more: Where the arguments come from? – risto Feb 06 '15 at 22:46
  • r2q is a hint to determine the optional quantum for a particular class. Quantum is a key parameter used by HTB(Hierarchical Token Bucket) to control borrowing. Normally, the correct quantum is calculated by HTB, not specified by the user. Tweaking this parameter can have tremendous effects on borrowing and shaping under contention. – Ricardo Feb 06 '15 at 23:02
  • the arguments $1, $2, and $3 are coming from whatever app is calling the script in your phone. In my case I just hardcode them in similar scripts when I know the exact bandwidth I have to start with. I suspect your phone has an app that tweaks these values, maybe you can poke around and find it. – Ricardo Feb 06 '15 at 23:06
  • I added an update above that explains how to see all the traffic shaping statistics. – Ricardo Feb 06 '15 at 23:14
  • How can check where the arguments come from,as u said what app is calling the script,i cant find a way to do it. – risto Feb 06 '15 at 23:41
  • 2
    the question was 'what is the purpose of the script?' I believe I answered it correctly. I have no idea what program is running the script, you would have to put an audit on the file to see who is calling it. – Ricardo Feb 07 '15 at 00:08