iptables优化 – 状态新?

我有一套实施VIP行为的规则 – 全部在nat表中。 显然我想尽可能less的处理数据包。

我不清楚“-m state –state NEW”是否在这种情况下是一种优化。 将build立的连接上的数据包将“快速path”?

我还可以做其他事情来优化一系列IP:端口比较和“-j DNAT”规则吗?

编辑:示例表

-t nat -A OUTPUT -j VIPS -t nat -A VIPS -d 10.0.154.213/32 -p tcp -m tcp -dport 80 -j SVC-FOO -t nat -A VIPS -d 10.0.140.123/32 -p tcp -m tcp -dport 80 -j SVC-BAR -t nat -A VIPS -d 10.0.221.241/32 -p tcp -m tcp -dport 80 -j SVC-QUX # ... could be hundreds of these # each SVC-* looks something like this -t nat -A SVC-FOO -m statistic --mode random --probability 0.5000000000 -j EP-FOO-1 -t nat -A SVC-FOO -j EP-FOO-2 # each EP-*-* looks something like this -t nat -A EP-FOO-2 -j DNAT --to-destination 10.244.2.7:9376 

这有点复杂,有几种方法可以在SVC-FOO链上完成,而EP- *链可能有LOG或MARK规则。

我的问题是,在贵宾链中寻找新的状态是否有帮助? 还是要进行其他优化?

不知道你的整个表是什么样的,很难肯定地说,但是当然匹配NEW包的规则并且把它们发送到另一个链上会加速对作为已知已build立连接一部分的包的处理。

一般来说,要通过一系列排他性匹配来达到iptables规则的最小数量,需要构build一个地址查找的“树”,如果你有一个/ 24个IPv4地址匹配,你可以build立一个8级的树,看起来像这样:

 iptables -t nat -N slash24 iptables -t nat -I PREROUTING -d 192.0.2.0/24 -j slash24 iptables -t nat -N slash25_0 iptables -t nat -N slash25_128 iptables -I slash24 -d 192.0.2.0/25 -j slash25_0 iptables -I slash24 -d 192.0.2.128/25 -j slash25_128 iptables -t nat -N slash26_0 iptables -t nat -N slash26_64 iptables -t nat -N slash26_128 iptables -t nat -N slash26_192 iptables -I slash25_0 -d 192.0.2.0/26 -j slash26_0 iptables -I slash25_0 -d 192.0.2.64/26 -j slash26_64 iptables -I slash25_128 -d 192.0.2.128/26 -j slash26_128 iptables -I slash25_128 -d 192.0.2.192/26 -j slash26_192 [etc etc etc] 

这不是你想要手工完成的,但是如果你有大量的规则(希望)以编程的方式进行pipe理,那么你可以编写脚本来创build整个不敬虔的怪物。

一旦你的规则达到了/ 32匹配级别,那么你把这个主机的所有规则放在一个链中 – 不幸的是,你不能在端口上播放同样types的“子网匹配”游戏。 希望你不要试图根据端口来自哪个端口来连接6万多台不同的机器。