Iptables性能測(cè)試
iptables我們經(jīng)常要用的,我將要通過實(shí)驗(yàn)測(cè)下Iptables性能。

宿主機(jī)指XEN服務(wù)器,VM1和VM2都是XEN上的兩個(gè)虛擬機(jī),我們這次的性能測(cè)試要壓VM1.
iptables的規(guī)則是在vm1的網(wǎng)橋上的,
通過對(duì)XEN服務(wù)器上的iptables的vm1鏈定義過濾規(guī)則,來測(cè)試不同規(guī)則數(shù)量對(duì)XEN服務(wù)器系統(tǒng)資源的影響.
場(chǎng)景一:
下圖是壓力源,選擇的是Telnet的場(chǎng)景,從三臺(tái)壓力機(jī)對(duì)xm list中的vm1進(jìn)行壓力測(cè)試,每個(gè)鏈接都是長(zhǎng)鏈接,共204個(gè)鏈接.

VM1上規(guī)則如下:
[root@localhost ~]# iptables-save |head -n100
# Generated by iptables-save v1.3.5 on Mon Nov 9 16:17:38 2009
*nat
:PREROUTING ACCEPT [11544456:847083593]
:POSTROUTING ACCEPT [1787885:91787925]
:OUTPUT ACCEPT [2030:122721]
COMMIT
# Completed on Mon Nov 9 16:17:38 2009
# Generated by iptables-save v1.3.5 on Mon Nov 9 16:17:38 2009
*filter
:INPUT ACCEPT [26255:1538953]
:FORWARD ACCEPT [86828026:3584385134]
:OUTPUT ACCEPT [24707:11914350]
:vm1 - [0:0]
-A FORWARD -m physdev --physdev-in peth0 --physdev-out vif10.0 -j vm1
-A vm1 -p tcp -m state --state RELATED,ESTABLISHED -j RETURN
-A vm1 -s 192.168.1.1 -p tcp -j ACCEPT
-A vm1 -s 192.168.1.2 -p tcp -j ACCEPT
-A vm1 -s 192.168.1.3 -p tcp -j ACCEPT
-A vm1 -s 192.168.1.4 -p tcp -j ACCEPT
-A vm1 -s 192.168.1.5 -p tcp -j ACCEPT
-A vm1 -s 192.168.1.6 -p tcp -j ACCEPT
.............
[root@localhost ~]# iptables-save |tail -n10
-A vm1 -s 192.168.139.13 -p tcp -j ACCEPT
-A vm1 -s 192.168.139.14 -p tcp -j ACCEPT
-A vm1 -s 192.168.139.15 -p tcp -j ACCEPT
-A vm1 -s ! 192.168.0.0/255.255.0.0 -p tcp -m tcp --dport 22 -j ACCEPT
-A vm1 -s ! 192.168.0.0/255.255.0.0 -p tcp -m tcp --dport 12345 -j ACCEPT
-A vm1 -s ! 192.168.0.0/255.255.0.0 -p tcp -m tcp --dport 10115 -j ACCEPT
-A vm1 -j ACCEPT
-A vm1 -j DROP
COMMIT
# Completed on Mon Nov 9 16:18:45 2009
規(guī)則數(shù)量:
[root@localhost ~]# iptables-save |grep "A vm1 -s" -c
34380
宿主機(jī)系統(tǒng)資源截圖:

在這張圖里面可以看到cpu時(shí)間花在softirq上有20.6%之多,
#p#
壓力執(zhí)行過程中iptables執(zhí)行狀態(tài)
[root@localhost ~]# iptables -vnL|head -n 10
Chain INPUT (policy ACCEPT 12078 packets, 792K bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 81M packets, 3330M bytes)
pkts bytes target prot opt in out source destination
116K 9011K vm1 all -- * * 0.0.0.0/0 0.0.0.0/0 PHYSDEV match --physdev-in peth0 --physdev-out vif10.0
Chain OUTPUT (policy ACCEPT 10612 packets, 8726K bytes)
pkts bytes target prot opt in out source destination
[root@localhost ~]# iptables -vnL|tail -n 5
0 0 ACCEPT tcp -- * * !192.168.0.0/16 0.0.0.0/0 tcp dpt:22
0 0 ACCEPT tcp -- * * !192.168.0.0/16 0.0.0.0/0 tcp dpt:12345
0 0 ACCEPT tcp -- * * !192.168.0.0/16 0.0.0.0/0 tcp dpt:10115
14783 1167K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
可以看到都被倒數(shù)第二條規(guī)則接收了.
附加信息:
vm1的數(shù)據(jù)流量:
root@ubuntu:~# ifstat
eth0
KB/s in KB/s out
2634.01 2632.44
2664.86 2662.29
2635.10 2633.90
2600.32 2599.70
2556.24 2553.81
2679.63 2679.75
2674.94 2674.52
2682.71 2678.81
2690.86 2689.16
2643.59 2641.61
2632.98 2630.52
2615.44 2615.29
壓力源和VM1的網(wǎng)絡(luò)鏈接狀態(tài)(壓力機(jī)上):
root@ubuntu:~# netstat -an|grep 10.2.226
tcp 0 0 10.2.226.221:10115 10.2.226.16:21482 TIME_WAIT
tcp 0 0 10.2.226.221:10115 10.2.226.15:26357 TIME_WAIT
tcp 0 1 10.2.226.221:12345 10.2.226.42:54471 ESTABLISHED
tcp 0 0 10.2.226.221:10115 10.2.226.16:21483 TIME_WAIT
tcp 0 0 10.2.226.221:10115 10.2.226.15:26356 TIME_WAIT
tcp 0 1 10.2.226.221:12345 10.2.226.42:54470 ESTABLISHED
tcp 0 0 10.2.226.221:10115 10.2.226.16:21480 TIME_WAIT
tcp 0 0 10.2.226.221:10115 10.2.226.15:26359 TIME_WAIT
tcp 0 1 10.2.226.221:12345 10.2.226.42:54469 ESTABLISHED
tcp 0 0 10.2.226.221:10115 10.2.226.16:21481 TIME_WAIT
tcp 0 0 10.2.226.221:10115 10.2.226.15:26358 TIME_WAIT
tcp 0 1 10.2.226.221:12345 10.2.226.42:54468 ESTABLISHED
壓力源和VM1之間連接數(shù):
root@ubuntu:~# netstat -an|grep 10.2.226 -c
206
root@ubuntu:~# netstat -an|grep 10.2.226 |wc -l
206
測(cè)試場(chǎng)景二:
壓力源不變,
iptables規(guī)則邏輯不變,
更改規(guī)則數(shù)量為:
[root@localhost ~]# iptables-save |grep "A vm1 -s" -c
252
此時(shí)宿主機(jī)系統(tǒng)資源截圖:

softirq占用了cpu的19%,也不少么!
這個(gè)試驗(yàn)證實(shí)了在該測(cè)試環(huán)境中,iptables的規(guī)則數(shù)對(duì)iptables主機(jī)的系統(tǒng)資源占用并無多大影響.
不過還兩個(gè)問題還需要繼續(xù)驗(yàn)證:
一:
目前這個(gè)場(chǎng)景的iptables只有一條接收所有請(qǐng)求.
如果有個(gè)場(chǎng)景的N條規(guī)則,每條規(guī)則都接受M條請(qǐng)求,會(huì)不會(huì)跟現(xiàn)在被測(cè)場(chǎng)景的消耗資源情況一樣?
二:
softirq占用的CPU是都給iptables使用去了么?還是被XEN server使用了?
通過文章的介紹和實(shí)驗(yàn)的測(cè)試,我們可以肯定的是iptables的性能還是不錯(cuò)的!
【編輯推薦】
- iptables 簡(jiǎn)單學(xué)習(xí)筆記
- 在Red Hat上安裝iptables
- 搭建基于netfilter/iptables的實(shí)驗(yàn)環(huán)境
- iptables nat實(shí)驗(yàn)
- 如何使用 IPTables
- iptables配置工具
- iptables 添加模塊HOWTO