Q:

How to use linux tc with tcp applications

I have a 1Gb network and I need to run certain TCP performance tests by limiting the rate to 100MB and then 1Gb. My understanding is that hierarchical token bucket (htb) can be used to achieve this as shown below
tc qdisc add dev eth0 root handle 1: htb default 12
tc class add dev eth0 parent 1: classid 1:1 htb rate 1gbps ceil 1gbps
tc class add dev eth0 parent 1: classid 1:10 htb rate 100mbps ceil 100mbps

This should create two root classes 1:1 and 1:10, with 1gb and 100mb bandwidth respectively. Please correct me if my understanding is wrong.
Next, how do i use the above classes in my tcp application? For example when i run my application, which should use only 100 Mb, do i need to specify something in socket options, such that any messages sent by the application now belong to class 1:1 and hence get rate limited to 100 Mb?

A:

Use a traffic control matching rule and an ip tables rule to mark the traffic.
tc filter add dev eth0 protocol ip parent 1:0 prio 1 handle 1 fw flowid 1:10
iptables -A OUTPUT -i eth0 -p tcp -j MARK --set-mark 1

Will put all your TCP outbound packets in the 100 megabits per second queue you have.
Since you want to match just one application, let us pretend that your application has a TCP socket listening on port 7654 that all connections are made through. The iptables rule would look more like this
iptables -A OUTPUT -i eth0 -p tcp -m multiport --sports 7654 -j MARK --set-mark 1

Also you should make the default in your tc qdisc command the 1 Gbps queue, so that normal traffic on the system is not affecting your test in any way.

