iptables 规则对性能的影响

本文探讨了iptables中大量的filter input规则如何影响MySQL数据库的性能。通过测试,揭示了这种网络过滤规则对SQL查询速度和数据库整体运行效率的潜在影响。

最近测试了下,iptables有很多的filter input规则的情况下,会对mysql本身的性能的影响.


数据准备:

创建iptable filter 规则

iptables -t filter -N RULEADMIN
iptables -t filter -I RULEADMIN -s 10.250.14.0/24  -j ACCEPT
iptables -t filter -I RULEADMIN -s 10.242.232.0/24  -j ACCEPT
iptables -t filter -A RULEADMIN -j DROP

iptables -t filter -N RULE3000
iptables -t filter -I RULE3000 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3000 -j RULEADMIN
iptables -t filter -N RULE3001
iptables -t filter -I RULE3001 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3001 -j RULEADMIN
iptables -t filter -N RULE3002
iptables -t filter -I RULE3002 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3002 -j RULEADMIN
iptables -t filter -N RULE3003
iptables -t filter -I RULE3003 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3003 -j RULEADMIN
iptables -t filter -N RULE3004
iptables -t filter -I RULE3004 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3004 -j RULEADMIN
iptables -t filter -N RULE3005
iptables -t filter -I RULE3005 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3005 -j RULEADMIN
iptables -t filter -N RULE3006
iptables -t filter -I RULE3006 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3006 -j RULEADMIN
iptables -t filter -N RULE3007
iptables -t filter -I RULE3007 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3007 -j RULEADMIN
iptables -t filter -N RULE3008
iptables -t filter -I RULE3008 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3008 -j RULEADMIN
iptables -t filter -N RULE3009
iptables -t filter -I RULE3009 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3009 -j RULEADMIN
iptables -t filter -N RULE3010
iptables -t filter -I RULE3010 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3010 -j RULEADMIN
iptables -t filter -N RULE3011
iptables -t filter -I RULE3011 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3011 -j RULEADMIN
iptables -t filter -N RULE3012
iptables -t filter -I RULE3012 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3012 -j RULEADMIN
iptables -t filter -N RULE3013
iptables -t filter -I RULE3013 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3013 -j RULEADMIN
iptables -t filter -N RULE3014
iptables -t filter -I RULE3014 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3014 -j RULEADMIN
iptables -t filter -N RULE3015
iptables -t filter -I RULE3015 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3015 -j RULEADMIN
iptables -t filter -N RULE3016
iptables -t filter -I RULE3016 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3016 -j RULEADMIN
iptables -t filter -N RULE3017
iptables -t filter -I RULE3017 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3017 -j RULEADMIN
iptables -t filter -N RULE3018
iptables -t filter -I RULE3018 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3018 -j RULEADMIN
iptables -t filter -N RULE3019
iptables -t filter -I RULE3019 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3019 -j RULEADMIN
iptables -t filter -N RULE3020
iptables -t filter -I RULE3020 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3020 -j RULEADMIN
iptables -t filter -N RULE3021
iptables -t filter -I RULE3021 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3021 -j RULEADMIN
iptables -t filter -N RULE3022
iptables -t filter -I RULE3022 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3022 -j RULEADMIN
iptables -t filter -N RULE3023
iptables -t filter -I RULE3023 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3023 -j RULEADMIN
iptables -t filter -N RULE3024
iptables -t filter -I RULE3024 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3024 -j RULEADMIN
iptables -t filter -N RULE3025
iptables -t filter -I RULE3025 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3025 -j RULEADMIN
iptables -t filter -N RULE3026
iptables -t filter -I RULE3026 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3026 -j RULEADMIN
iptables -t filter -N RULE3027
iptables -t filter -I RULE3027 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3027 -j RULEADMIN
iptables -t filter -N RULE3028
iptables -t filter -I RULE3028 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3028 -j RULEADMIN
iptables -t filter -N RULE3029
iptables -t filter -I RULE3029 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3029 -j RULEADMIN
iptables -t filter -N RULE3030
iptables -t filter -I RULE3030 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3030 -j RULEADMIN
iptables -t filter -N RULE3031
iptables -t filter -I RULE3031 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3031 -j RULEADMIN
iptables -t filter -N RULE3032
iptables -t filter -I RULE3032 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3032 -j RULEADMIN
iptables -t filter -N RULE3033
iptables -t filter -I RULE3033 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3033 -j RULEADMIN
iptables -t filter -N RULE3034
iptables -t filter -I RULE3034 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3034 -j RULEADMIN
iptables -t filter -N RULE3035
iptables -t filter -I RULE3035 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3035 -j RULEADMIN
iptables -t filter -N RULE3036
iptables -t filter -I RULE3036 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3036 -j RULEADMIN
iptables -t filter -N RULE3037
iptables -t filter -I RULE3037 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3037 -j RULEADMIN
iptables -t filter -N RULE3038
iptables -t filter -I RULE3038 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3038 -j RULEADMIN
iptables -t filter -N RULE3039
iptables -t filter -I RULE3039 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3039 -j RULEADMIN
iptables -t filter -N RULE3040
iptables -t filter -I RULE3040 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3040 -j RULEADMIN
iptables -t filter -N RULE3041
iptables -t filter -I RULE3041 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3041 -j RULEADMIN
iptables -t filter -N RULE3042
iptables -t filter -I RULE3042 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3042 -j RULEADMIN
iptables -t filter -N RULE3043
iptables -t filter -I RULE3043 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3043 -j RULEADMIN
iptables -t filter -N RULE3044
iptables -t filter -I RULE3044 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3044 -j RULEADMIN
iptables -t filter -N RULE3045
iptables -t filter -I RULE3045 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3045 -j RULEADMIN
iptables -t filter -N RULE3046
iptables -t filter -I RULE3046 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3046 -j RULEADMIN
iptables -t filter -N RULE3047
iptables -t filter -I RULE3047 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3047 -j RULEADMIN
iptables -t filter -N RULE3048
iptables -t filter -I RULE3048 -s 10.1.147.147  -j ACCEPT
iptables -t filter -A RULE3048 -j RULEADMIN



iptables -t filter -I INPUT 1 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t filter -I INPUT 2 -d 10.1.154.0 -p tcp --dport 3000 -j RULE3001
iptables -t filter -I INPUT 2 -d 10.1.154.1 -p tcp --dport 3001 -j RULE3002
iptables -t filter -I INPUT 2 -d 10.1.154.2 -p tcp --dport 3002 -j RULE3003
iptables -t filter -I INPUT 2 -d 10.1.154.3 -p tcp --dport 3003 -j RULE3004
iptables -t filter -I INPUT 2 -d 10.1.154.4 -p tcp --dport 3004 -j RULE3005
iptables -t filter -I INPUT 2 -d 10.1.154.5 -p tcp --dport 3005 -j RULE3006
iptables -t filter -I INPUT 2 -d 10.1.154.6 -p tcp --dport 3006 -j RULE3007
iptables -t filter -I INPUT 2 -d 10.1.154.7 -p tcp --dport 3007 -j RULE3008
iptables -t filter -I INPUT 2 -d 10.1.154.8 -p tcp --dport 3008 -j RULE3009
iptables -t filter -I INPUT 2 -d 10.1.154.9 -p tcp --dport 3009 -j RULE3010
iptables -t filter -I INPUT 2 -d 10.1.154.10 -p tcp --dport 3010 -j RULE3011
iptables -t filter -I INPUT 2 -d 10.1.154.11 -p tcp --dport 3011 -j RULE3012
iptables -t filter -I INPUT 2 -d 10.1.154.12 -p tcp --dport 3012 -j RULE3013
iptables -t filter -I INPUT 2 -d 10.1.154.13 -p tcp --dport 3013 -j RULE3014
iptables -t filter -I INPUT 2 -d 10.1.154.14 -p tcp --dport 3014 -j RULE3015
iptables -t filter -I INPUT 2 -d 10.1.154.15 -p tcp --dport 3015 -j RULE3016
iptables -t filter -I INPUT 2 -d 10.1.154.16 -p tcp --dport 3016 -j RULE3017
iptables -t filter -I INPUT 2 -d 10.1.154.17 -p tcp --dport 3017 -j RULE3018
iptables -t filter -I INPUT 2 -d 10.1.154.18 -p tcp --dport 3018 -j RULE3019
iptables -t filter -I INPUT 2 -d 10.1.154.19 -p tcp --dport 3019 -j RULE3020
iptables -t filter -I INPUT 2 -d 10.1.154.20 -p tcp --dport 3020 -j RULE3021
iptables -t filter -I INPUT 2 -d 10.1.154.21 -p tcp --dport 3021 -j RULE3022
iptables -t filter -I INPUT 2 -d 10.1.154.22 -p tcp --dport 3022 -j RULE3023
iptables -t filter -I INPUT 2 -d 10.1.154.23 -p tcp --dport 3023 -j RULE3024
iptables -t filter -I INPUT 2 -d 10.1.154.24 -p tcp --dport 3024 -j RULE3025
iptables -t filter -I INPUT 2 -d 10.1.154.25 -p tcp --dport 3025 -j RULE3026
iptables -t filter -I INPUT 2 -d 10.1.154.26 -p tcp --dport 3026 -j RULE3027
iptables -t filter -I INPUT 2 -d 10.1.154.27 -p tcp --dport 3027 -j RULE3028
iptables -t filter -I INPUT 2 -d 10.1.154.28 -p tcp --dport 3028 -j RULE3029
iptables -t filter -I INPUT 2 -d 10.1.154.29 -p tcp --dport 3029 -j RULE3030
iptables -t filter -I INPUT 2 -d 10.1.154.30 -p tcp --dport 3030 -j RULE3031
iptables -t filter -I INPUT 2 -d 10.1.154.31 -p tcp --dport 3031 -j RULE3032
iptables -t filter -I INPUT 2 -d 10.1.154.32 -p tcp --dport 3032 -j RULE3033
iptables -t filter -I INPUT 2 -d 10.1.154.33 -p tcp --dport 3033 -j RULE3034
iptables -t filter -I INPUT 2 -d 10.1.154.34 -p tcp --dport 3034 -j RULE3035
iptables -t filter -I INPUT 2 -d 10.1.154.35 -p tcp --dport 3035 -j RULE3036
iptables -t filter -I INPUT 2 -d 10.1.154.36 -p tcp --dport 3036 -j RULE3037
iptables -t filter -I INPUT 2 -d 10.1.154.37 -p tcp --dport 3037 -j RULE3038
iptables -t filter -I INPUT 2 -d 10.1.154.38 -p tcp --dport 3038 -j RULE3039
iptables -t filter -I INPUT 2 -d 10.1.154.39 -p tcp --dport 3039 -j RULE3040
iptables -t filter -I INPUT 2 -d 10.1.154.40 -p tcp --dport 3040 -j RULE3041
iptables -t filter -I INPUT 2 -d 10.1.154.41 -p tcp --dport 3041 -j RULE3042
iptables -t filter -I INPUT 2 -d 10.1.154.42 -p tcp --dport 3042 -j RULE3043
iptables -t filter -I INPUT 2 -d 10.1.154.43 -p tcp --dport 3043 -j RULE3044
iptables -t filter -I INPUT 2 -d 10.1.154.44 -p tcp --dport 3044 -j RULE3045
iptables -t filter -I INPUT 2 -d 10.1.154.45 -p tcp --dport 3045 -j RULE3046
iptables -t filter -I INPUT 2 -d 10.1.154.46 -p tcp --dport 3046 -j RULE3047
iptables -t filter -I INPUT 2 -d 10.1.154.47 -p tcp --dport 3047 -j RULE3048
iptables -t filter -I INPUT 2 -d 10.1.154.48 -p tcp --dport 3048 -j RULE3000


iptables -t filter -N RULE3049
iptables -t filter -I RULE3049 -s 10.1.147.65  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.75  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.85  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.86  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.103  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.113  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.114  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.159  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.160  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.161  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.163  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.166  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.162  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.165  -j ACCEPT
iptables -t filter -I RULE3049 -s 10.1.147.0/24  -j ACCEPT
iptables -t filter -A RULE3049 -j RULEADMIN

iptables -t filter - INPUT 2 -d 10.1.147.3 -p tcp --dport 3306 -j RULE3049
--------------------------------


看上去有这么多的规则,其实,对于长连接来说,其实只经过了一条规则

iptables -t filter -I INPUT 1 -m state --state ESTABLISHED,RELATED -j ACCEPT
即对于所有的已经建立连接的tcp包,全部都accept.

测试方式:
1 . 单线程往向mysql发送sql,用长连接的方式,发送50000次,在有iptables的情况下,分别发送20次
2. 单线程往mysql发送同样的sql,50000次,分别发送20次


分别计算2组数据的平均每个请求的时间
带有iptables没有iptables
11.9962201110.40345907
11.6716718710.15265417
11.2455971210.30219007
11.22271810.40044904
11.4289860710.49085999
11.5912969110.43716884
11.6416900210.57864714
12.2421388610.42826486
11.8535718910.26615214
11.4098651410.40360284
12.1028342210.3410759
11.6061489610.42641878
11.2016820910.60911489
11.2748849410.63199115
11.3647999810.67307496
11.3625419110.58659887
11.6779501410.4637661
11.6607599310.313941
11.4325661710.29340601
11.2320959610.8853991
AVG
11.5610010110.45441175
每个请求消耗时间差:0.000022

测试结果,有iptables会比没有iptables多耗费22us左右的时间.
即下面这条filter规则大概需要22us
iptables -t filter -I INPUT 1 -m state --state ESTABLISHED,RELATED -j ACCEPT

nat 端口转发
11.63080692
11.90807295
12.46742797
12.33331704
11.80403209
12.104146
11.72256398
12.3218751
12.31458497
12.38100791
13.80361986
12.83216214
11.99734807
11.71055198
11.51495504
11.65256119
12.37192702
12.85852599
12.70576
12.76274395
avg
12.25989951
(12.25989951-11.56100101)/50000=14us

增加nat 表转换后,又差不多增加了14us
所以,按照测试结果,nat 表的效率要稍微好于filter表


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值