Monday, September 26, 2016

traffic and bandwidth control on linux

tc comes with iproute2, it has many features to  manipulate and control traffic in and out the linux system. with many features, it also becomes confusing and hard to use, if you are lazy in reading its document, there is a less complicated wrapper called tcconfig.  but it does not provide all the features of the tc.

1- diagram

internet <--  (ens33) tcconfig (ens38) <-- client (192.168.7.2)

tcconfig has two network interfaces, called, ens33 and ens38

1- install tcconfig
$ sudo pip install tcconfig

2- limit the bandwidth to only 1Mbps to client
$ sudo tcset --device ens38 --rate 1024k --network 192.168.7.2

3- set delay on the interface connect to client
$ sudo tcset --device ens38 --delay 100

4- set packets loss to 0.2% on the whole network
$ sudo tcset --device ens33 --loss 0.2

5- set rate limit of 1Mbps with loss packet of 0.2% to ssh protocol on network 192.168.7.0/24
$ sudo tcset --device ens38 --rate 1024k --loss 0.2 --network 192.168.7.0/24 --port 22

6- delete rule that you have create
$ sudo tcdel --device ens38
$ sudo tcdel --device ens33

7- and you want to lookup want tcconfig add to tc rules use the following command

- list qdisc
$ sudo  tc qdisc list
qdisc noqueue 0: dev lo root refcnt 2
qdisc pfifo_fast 0: dev ens33 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc prio 1aaf: dev ens38 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
qdisc netem 1ab9: dev ens38 parent 1aaf:1 limit 1000
qdisc tbf 20: dev ens38 parent 1ab9:1 rate 1Mbit burst 1600b lat 67.2ms


-list class
$ sudo tc class show dev ens38
class prio 1aaf:1 parent 1aaf: leaf 1ab9:
class prio 1aaf:2 parent 1aaf:
class prio 1aaf:3 parent 1aaf:
class netem 1ab9:1 parent 1ab9: leaf 20:
class tbf 20:1 parent 20: 


-list filter
$ tc filter show dev ens38
filter parent 1aaf: protocol ip pref 1 u32
filter parent 1aaf: protocol ip pref 1 u32 fh 801: ht divisor 1
filter parent 1aaf: protocol ip pref 1 u32 fh 801::800 order 2048 key ht 801 bkt 0 flowid 1aaf:1
  match c0a80700/ffffff00 at 16
filter parent 1aaf: protocol ip pref 2 u32
filter parent 1aaf: protocol ip pref 2 u32 fh 800: ht divisor 1
filter parent 1aaf: protocol ip pref 2 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1aaf:2
  match 00000000/00000000 at 16



- snapshot of effect of the limit to client with bandwidth only 1Mbps

No comments:

Post a Comment