IPTables加载大量规则的性能问题测试
时间:2005-12-20 来源:wenzk
今天作了一个测试,得到了插入一万条数据时iptables的性能数据。
- 先写了一个perl脚本:
#!/usr/bin/perl -w
# Author: Zhuohuan Li <[email protected]>
# Date: 2005-02-23my $MAX_NUM=10000;
#输出 iptables-restore 数据
#print_iptables_restore();#输出调用iptables的脚本程序
print_iptables();sub print_iptables_restore
{
print “# Generated by gen_rule.pl v1.0 on ” . `date`;
print ‘# by Zhuohuan Li <[email protected]> 2005-02-23′ . “ ”;
print “*filter ”;
print “:zixia - [0:0] ”;for ( $n=0; $n<$MAX_NUM; $n++ ){
print “-A zixia “
. random_rule() . “ ”;
}
print “COMMIT ”;
print “# Completed on ” . `date`;
}sub print_iptables
{
for ( $n=0; $n<$MAX_NUM; $n++ ){
print “iptables -A zixia “
. random_rule() . “ ”;
if ( 0==$n%100 ){
print “date ”;
}
}
}sub random_rule
{
return
” -p ” . random_protocol()
. ” -s ” . random_ip()
. ” –sport ” . random_port()
. ” -d ” . random_ip()
. ” –dport ” . random_port()
#. ” -m time –timestart ” . random_clocktime()
#. ” –timestop ” . random_clocktime()
#. ” –days ” . random_day()
. ” -j ” . random_target()}
sub random_ip
{
return int(rand(255)) . ‘.’ . int(rand(255)) . ‘.’ . int(rand(255)) . ‘.’ . int(rand(255)) ;
}sub random_port
{
return int(rand(65536));
}sub random_day
{
return (’Fri’,'Tue’,'Wed’,'Thu’,'Fri’,'Sat’,'Sun’)[int(rand(7))];
}sub random_clocktime
{
return (’0:00′,’1:00′,’2:00′,’3:00′,’4:00′,’5:00′,’6:00′,’7:00′,’8:00′,’9:00′,’10:00′,’11:00′,’12:00′,’13:00′,’14:00′,’15:00′,’16:00′,’17:00′,’18:00′,’19:00′,’20:00′,’21:00′,’22:00′,’23:00′)[int(rand(24))];
}sub random_protocol
{
return (’tcp’,'udp’)[int(rand(2))];
}sub random_target
{
return (’ACCEPT’,'DROP’,'RETURN’)[int(rand(3))];
} - 然后分别使用iptables-restore和iptables对系统进行导入10,000条数据的测试,速度结果如下:
tty:[1] jobs:[0] cwd:[~]
08:48 [[email protected]]# ./gen_rule.pl > 10k使用print_iptables_restore输出
tty:[1] jobs:[0] cwd:[~]
08:48 [[email protected]]# time iptables-restore < 10kreal 2m37.558s
user 0m35.586s
sys 1m52.955s使用print_iptables输出
tty:[1] jobs:[0] cwd:[~]
08:54 [[email protected]]# time sh 10k
real 14m49.825s
user 1m14.916s
sys 12m33.936s - iptables结论
数据量:10,000条规则
耗时:14分50秒
下图的横轴为系统中已经插入成功的规则条数,纵轴为在系统中已经存在一定数目的规则数的情况下,新加入100条规则所需时间。可以看出,随着系统中规则数的增加,新插入100条规则所需的时间急剧升高,从开始的1秒变为15秒左右。性能下降的非常厉害。
- iptables-restore结论
数据量:10,000条规则
耗时:2分38秒
iptables-restore因为比调用iptables省去10,000次系统fork和程序初始化,所以速度比iptables快了6倍
记得scaner说过,iptables插入超过几万条规则后,没插入一条都非常非常的慢。
iptables < iptables-restore
最好的办法,是写一个hack,将所有的规则整理成为netfilter内存数据结构,然后一次性的写进去。如果想用海量的iptables规则,看来还是需要仔细考虑优化问题。
测试的硬件配置:P4 2G左右