IFB
本文翻譯自Linux官方IFB文檔
IFB(中介功能塊設備)是IMQ(中介隊列設備)的繼任者,IMQ從來沒有被集成過,IFB擁有IMQ的優點,在SMP上更加清晰明了,并且代碼量縮減了非常多,舊的中介設備功能仍然保留,但在你使用actions時你需要新的
目錄
IFB使用場景
典型應用
跑一個小測試
IFB樣例
IFB的依賴
IFB樣例
IFB使用場景
據我所知,如下場景是人們使用IMQ的理由:
隊列/策略是針對單個網卡而不是應用到整個系統的,而IMQ允許多個設備共享隊列/策略
允許入境流量被整流而不僅僅是被丟棄。我不知道有什么研究能說明丟包比整流更差,如果有我會十分感興趣。
思科的Comparing Traffic Policing and Traffic Shaping for Bandwidth Limiting表明整流(排隊)比策略(限流)更好,因為策略會丟棄多余的包,限制TCP的窗口大小并且減少基于TCP的流的輸出速率
現實中幾乎所有的互聯網流量都是基于TCP的
非常有意思的應用:如果你在提供P2P服務,你可能會想要優先處理本地流量而不是其他使用你的系統上/下載的流量。所以基于狀態的QoS是一種解決方案,人們用在本地流量入口之前掛載IMQ的方式實現了它。我認為這是Linux上非常有特點的應用(不僅僅是IMQ)
不過我不會再使用netfilter hooks的方式實現這個,我也不會認為這值的我修改IFB獲取三層網絡的信息來實現它
替代的方案是使用一個跟蹤連接的action。這個action會選擇性的在入境數據包上查詢/創建連接跟蹤狀態。數據包因此可以根據發生了什么來被重定向到IFB。如果我們發現它們是已知的狀態我們就可以把它發送到不同于還沒有狀態的數據包的隊列里。這取決于管理員制定的規則。
現在這個功能還不存在,我決定不在補丁上release它,如果有強烈要求我會增加這個特性
你現在可以用IFB做的是actions
假設你在來自192.168.200.200/32上的數據包上限流到100kbps
tc filter add dev eth0 parent 1: protocol ip prio 10 u32
match ip src 192.168.200.200/32 flowid 1:2
action police rate 100kbit burst 90k drop
如果你在eth0上運行tcpdump,你會看到所有來自于192.168.200.200/32的數據包是否被丟棄。擴展這條規則來只觀察這些包:
tc filter add dev eth0 parent 1: protocol ip prio 10 u32
match ip src 192.168.200.200/32 flowid 1:2
action police rate 10kbit burst 90k drop
action mirred egress mirror dev ifb0
現在在ifb0上開啟tcpdump就可以只觀察這些包:
tcpdump -n -i ifb0 -x -e -t
這是一個非常好的debug和log的接口
如果你用重定向代替鏡像,這些包會進入黑洞再也無法出來,這個重定向的行為會在新的補丁上被修改(但是鏡像沒有問題)
典型應用
你可以使用新的補丁來實現以前用IMQ實現的功能
export TC="/sbin/tc"
$TC qdisc add dev ifb0 root handle 1: prio
$TC qdisc add dev ifb0 parent 1:1 handle 10: sfq
$TC qdisc add dev ifb0 parent 1:2 handle 20: tbf rate 20kbit buffer 1600 limit 3000
$TC qdisc add dev ifb0 parent 1:3 handle 30: sfq
$TC filter add dev ifb0 protocol ip pref 1 parent 1: handle 1 fw classid 1:1
$TC filter add dev ifb0 protocol ip pref 2 parent 1: handle 2 fw classid 1:2
ifconfig ifb0 up
$TC qdisc add dev eth0 ingress
# redirect all IP packets arriving in eth0 to ifb0
# use mark 1 --> puts them onto class 1:1
$TC filter add dev eth0 parent ffff: protocol ip prio 10 u32
match u32 0 0 flowid 1:1
action ipt -j MARK --set-mark 1
action mirred egress redirect dev ifb0
跑一個小測試
從另一臺機器ping過來,這樣你可以獲得入境流量:
[root@jzny action-tests]# ping 10.22
PING 10.22 (10.0.0.22): 56 data bytes
64 bytes from 10.0.0.22: icmp_seq=0 ttl=64 time=2.8 ms
64 bytes from 10.0.0.22: icmp_seq=1 ttl=64 time=0.6 ms
64 bytes from 10.0.0.22: icmp_seq=2 ttl=64 time=0.6 ms
--- 10.22 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.6/1.3/2.8 ms
[root@jzny action-tests]#
現在看一下stats:
[root@jmandrake]:~# $TC -s filter show parent ffff: dev eth0
filter protocol ip pref 10 u32
filter protocol ip pref 10 u32 fh 800: ht divisor 1
filter protocol ip pref 10 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1
match 00000000/00000000 at 0
action order 1: tablename: mangle hook: NF_IP_PRE_ROUTING
target MARK set 0x1
index 1 ref 1 bind 1 installed 4195sec used 27sec
Sent 252 bytes 3 pkts (dropped 0, overlimits 0)
action order 2: mirred (Egress Redirect to device ifb0) stolen
index 1 ref 1 bind 1 installed 165 sec used 27 sec
Sent 252 bytes 3 pkts (dropped 0, overlimits 0)
[root@jmandrake]:~# $TC -s qdisc
qdisc sfq 30: dev ifb0 limit 128p quantum 1514b
Sent 0 bytes 0 pkts (dropped 0, overlimits 0)
qdisc tbf 20: dev ifb0 rate 20Kbit burst 1575b lat 2147.5s
Sent 210 bytes 3 pkts (dropped 0, overlimits 0)
qdisc sfq 10: dev ifb0 limit 128p quantum 1514b
Sent 294 bytes 3 pkts (dropped 0, overlimits 0)
qdisc prio 1: dev ifb0 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
Sent 504 bytes 6 pkts (dropped 0, overlimits 0)
qdisc ingress ffff: dev eth0 ----------------
Sent 308 bytes 5 pkts (dropped 0, overlimits 0)
[root@jmandrake]:~# ifconfig ifb0
ifb0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
UP BROADCAST RUNNING NOARP MTU:1500 Metric:1
RX packets:6 errors:0 dropped:3 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:32
RX bytes:504 (504.0 b) TX bytes:252 (252.0 b)
偽設備(指IFB)仍然表現得像以前一樣
總結
- 上一篇: 如何在家里制作美味的烤豆腐?
- 下一篇: 电饭煲做鸡汤放多少水?