How to bandwidth monitoring/limiting and traffic shape in Linux (using iptables, tc/netem/wondershaper, trickle, …)

You can apply bandwidth allocation for each port. You can also apply traffic shaping to outgoing or forwarding traffic.

  • tc@man show / manipulate traffic control settings. Most of the time we use iptables to set up a firewall on a machine, but iptables also provides packet and byte counters.
## add latency
# slowdown network connections to localhost by 500ms
$ tc qdisc add dev lo root handle 1:0 netem delay 500msec

# slow down traffic by 200 ms
$ tc qdisc add dev eth0 root netem delay 200ms

# list all rules
$ tc -s qdisc ls dev eth0

## control the use of the outbound bandwidth on a given link use HTB
# delete all rules
$ tc qdisc del dev eth1 root
# turn on queuing discipline
$ tc qdisc add dev eth1 root handle 1:0 htb default 10
# define a class with limitations i.e. set the allowed bandwidth to 512 Kilobytes and burst bandwidth to 640 Kilobytes for port 80
$ tc class add dev eth1 parent 1:0 classid 1:10 htb rate 512kbps ceil 640kbps prio 0
# use iptables mangle rule
$ iptables -A OUTPUT -t mangle -p tcp --sport 80 -j MARK --set-mark 10
$ service iptables save
# assign it to appropriate qdisc
$ tc filter add dev eth1 parent 1:0 prio 0 protocol ip handle 10 fw flowid 1:10

# monitor
$ tc -s -d class show dev eth1

## using netem to emulates variable delay, loss, duplication and re-ordering
# added delay to be 100ms ± 10ms
$ tc qdisc change dev eth0 root netem delay 100ms 10ms
# causes 1/10th of a percent (i.e 1 out of 1000) packets to be randomly dropped
$ tc qdisc change dev eth0 root netem loss 0.1%
# introduces a single bit error at a random offset in the packet
$ tc qdisc change dev eth0 root netem corrupt 0.1%

from Howto traffic shape in Linux tc, Howto bandwidth limiting with IP masquerade, Howto Traffic-Control and bandwidth monitoring with iptables.

  • wondershaper is a shell script which uses tc to define traffic shaping and QoS for a specific network interface. Outgoing traffic is shaped by being placed in queues with different priorities, while incoming traffic is rate-limited by packet dropping.
## install
$ sudo apt-get install wondershaper | sudp yum install wondershaper (EPEL)

## usage
$ sudo wondershaper <interface> <download-rate> <upload-rate>
# limit download/upload bandwidth for eth0 to 1000Kbits and 500Kbits
$ sudo wondershaper eth0 1000 500
# clear limit
$ sudo wondershaper clear eth0

from How to limit network bandwidth on Linux

  • trickle@man a lightweight userspace bandwidth shaper.
## install 
$ sudo apt-get install trickle | sudo yum install trickle (EPEL)

## usage
$ trickle -d <download-rate> -u <upload-rate> <command>
# launch ncftp limiting its upload/download to 10KBs/20KBs
$ trickle -u 10 -d 20 ncftp
# launch firefox w/ download limit to 300Kbs
trickle -d 300 firefox %u

from Trickle: Linux Limit A Specific User’s Shell Account Network Bandwidth Using Bash Shell

  • clumsy traffic capture/lag/drop/tamper for Windows, based on WinDivert library.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s