tc,iptablesの使い方メモ

# tc qdisc add dev em1 root handle 1: htb default 30

# tc class add dev em1 parent 1: classid 1:1 htb rate 6mbit burst 15k

# tc class add dev em1 parent 1:1 classid 1:10 htb rate 5mbit burst 15k

# tc class add dev em1 parent 1:1 classid 1:20 htb rate 3mbit ceil 6mbit burst 15k

# tc class add dev em1 parent 1:1 classid 1:30 htb rate 1kbit ceil 6mbit burst 15k

# tc qdisc add dev em1 parent 1:10 handle 10: sfq perturb 10

# tc qdisc add dev em1 parent 1:20 handle 20: sfq perturb 10

# tc qdisc add dev em1 parent 1:30 handle 30: sfq perturb 10

# tc filter add dev em1 protocol ip parent 1:0 prio 1 u32 match ip tos 0x10 0xff flowid 1:10

# tc filter add dev em1 protocol ip parent 1:0 prio 1 u32 match ip tos 0x14 0xff flowid 1:20

# iptables -A OUTPUT -t mangle -p tcp --dport 5001 -j TOS --set-tos 0x10

# iptables -A OUTPUT -t mangle -p tcp --dport 5002 -j TOS --set-tos 0x14

[状態参照]

tc { qdisc | class | filter | action | monitor } show dev em1

tc -s qdisc ls dev br0


[Priority Queueの設定の仕方/解説]
クラスフルなキューイング規則
http://linuxjf.sourceforge.jp/JFdocs/Adv-Routing-HOWTO/lartc.qdisc.classful.html
PRIO queuing disciple
http://opalsoft.net/qos/DS-23.htm
6.2.4. Example: Linux Packet Scheduling
http://d3s.mff.cuni.cz/~ceres/sch/osy/text/ch06s02s04.html



--- 追記 ---
tc qdisc add dev p1p1 root handle 1: htb default 30
tc class add dev p1p1 parent 1: classid 1:1 htb rate 1gbit burst 100m
tc class add dev p1p1 parent 1:1 classid 1:10 htb rate 1gbit burst 100m
tc class add dev p1p1 parent 1:1 classid 1:20 htb rate 1gbit burst 100m
tc class add dev p1p1 parent 1:1 classid 1:30 htb rate 1gbit burst 100m
tc qdisc add dev p1p1 parent 1:10 handle 10: pfifo limit 5000
tc qdisc add dev p1p1 parent 1:20 handle 20: pfifo limit 5000
tc qdisc add dev p1p1 parent 1:30 handle 30: pfifo limit 5000
tc filter add dev p1p1 protocol ip parent 1:0 prio 1 u32 match ip tos 0x10 0xff flowid 1:10
tc filter add dev p1p1 protocol ip parent 1:0 prio 1 u32 match ip tos 0x14 0xff flowid 1:20
iptables -A OUTPUT -t mangle -p tcp --dport 5001 -j TOS --set-tos 0x10
iptables -A OUTPUT -t mangle -p tcp --dport 5002 -j TOS --set-tos 0x14

--- 追記2 ---
iptables -A OUTPUT -t mangle -p udp --dport 5001 -j TOS --set-tos 0x04
iptables -A OUTPUT -t mangle -p udp --dport 5002 -j TOS --set-tos 0x08
iptables -A OUTPUT -t mangle -p udp --dport 5003 -j TOS --set-tos 0x0c

--- 追記3 ---
burstサイズが小さすぎると,パケットが詰まる.
例えば,
tc qdisc add dev em1 root tbf rate 800mbit burst 1540 latency 10ms
と設定して,iperfするとパケットが詰まって送信できなくなる.
なぜ??詰まっても10msしたら破棄されるのでは????

--- 追記4 ---
tcの設定例
tc qdisc add dev em1 root tbf rate 100mbit burst 10m latency 10ms
[ 146.466286] Debug:
[ 146.466286] qopt->
[ 146.466286] limit=10610760
[ 146.466286] mtu=0
[ 146.466286] buffer=13107187
[ 146.466286]
[ 146.466286] q->
[ 146.466286] limit=10610760(qopt->limit)
[ 146.466286] mtu=0(PSCHED_TICKS2NS(qopt->mtu))
[ 146.466286] max_size=2047
[ 146.466286] buffer=838859968(PSCHED_TICKS2NS(qopt->buffer))
[ 146.466286] tokens=buffer
[ 146.466286] ptokens=mtu
[ 146.466286] q->rate.
[ 146.466286] rate_bytes_ps=100000000
[ 146.466286] mult=2621440
[ 146.466286] overhead=0
[ 146.466286] linklayer=0
[ 146.466286] shift=15

--- 追記5 ---
tbfはrateやburstなどのパラメータを適切な値で設定しないと,指定した帯域で帯域制限が出来なかった(iperfで計測).なぜ?
TCPで計測していたからっぽい.輻輳制御機能の影響で帯域が抑えられていたかも?UDPでは予想通りの値が出た.
以下,一応うまくいったっぽいパラメータ
tc qdisc change dev em1 root tbf rate 500mbit burst 26mb latency 100ms
tc qdisc change dev em1 root tbf rate 10mbit burst 1mb latency 100ms

--- 追記6 ---
tc qdisc change dev em1 root tbf rate 500mbit burst 26mb latency 100ms
[1690248.657933] RTvNIC Debug:
[1690248.657933] qopt->
[1690248.657933] limit=33512976
[1690248.657933] mtu=0
[1690248.657933] buffer=6815734
[1690248.657933]
[1690248.657933] q->
[1690248.657933] limit=33512976(qopt->limit)
[1690248.657933] mtu=0(PSCHED_TICKS2NS(qopt->mtu))
[1690248.657933] max_size=2047
[1690248.657933] buffer=436206976(PSCHED_TICKS2NS(qopt->buffer))
[1690248.657933] tokens=buffer
[1690248.657933] ptokens=mtu
[1690248.657933] q->rate.
[1690248.657933] rate_bytes_ps=500000000
[1690248.657933] mult=524288
[1690248.657933] overhead=0
[1690248.657933] linklayer=0
[1690248.657933] shift=15
[1691734.753893] RTvNIC Debug:
[1691734.753893] qopt->
[1691734.753893] limit=33512976
[1691734.753893] mtu=0
[1691734.753893] buffer=6815734
[1691734.753893]
[1691734.753893] q->
[1691734.753893] limit=33512976(qopt->limit)
[1691734.753893] mtu=0(PSCHED_TICKS2NS(qopt->mtu))
[1691734.753893] max_size=2047
[1691734.753893] buffer=436206976(PSCHED_TICKS2NS(qopt->buffer))
[1691734.753893] tokens=buffer
[1691734.753893] ptokens=mtu
[1691734.753893] q->rate.
[1691734.753893] rate_bytes_ps=500000000
[1691734.753893] mult=524288
[1691734.753893] overhead=0
[1691734.753893] linklayer=0
[1691734.753893] shift=15

--- 追記7 ---
tc qdisc add dev em1 root handle 1: prio
tc qdisc add dev em1 parent 1:1 handle 10: bfifo
tc qdisc add dev em1 parent 1:2 handle 20: bfifo
tc qdisc add dev em1 parent 1:3 handle 30: bfifo

tc filter add dev em1 parent 1:0 protocol ip prio 0 u32 match ip dst 192.168.5.1 flowid 1:1

iperf -c 192.168.5.1 -u