Skip to content

Instantly share code, notes, and snippets.

@itxx00
Created June 24, 2016 03:14
Show Gist options
  • Save itxx00/de5178e2566e270c4567daff62094582 to your computer and use it in GitHub Desktop.
Save itxx00/de5178e2566e270c4567daff62094582 to your computer and use it in GitHub Desktop.
network interface interrupts customize
# https://www.qcloud.com/doc/product/215/2238
3. 配置优化
公网网关主机会默认配置iptables的nat规则,以及打开kernel的ip_forward,基本的公网网关功能已经完全具备。建议经过下述配置,以达到更好的性能。
1) 通过以下命令将net.ipv4.ip_forward配置写到/etc/sysctl.conf文件中
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
2) 通过以下命令将nf_conntrack配置参数调大
echo "echo 1048576 > /proc/sys/net/netfilter/nf_conntrack_max" >> /etc/rc.local
echo "echo 262144 > /sys/module/nf_conntrack/parameters/hashsize" >> /etc/rc.local
3) 设置转发的nat规则
echo "iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE" >> /etc/rc.local
4) 关闭时间戳选项
echo "iptables -t mangle -A POSTROUTING -p tcp -j TCPOPTSTRIP --strip-options timestamp" >> /etc/rc.local
5) 设置公网网关的rps
在/usr/local/sbin/目录下新建脚本set_rps.sh,将以下代码写入脚本中:
#!/bin/bash
mask=0
i=0
cpu_nums=`cat /proc/cpuinfo |grep processor |wc -l`
if(($cpu_nums==0));then
exit 0
fi
nic_queues=`cat /proc/interrupts |grep -i virtio0-input |wc -l`
if(($nic_queues==0));then
exit 0
fi
echo "cpu number" $cpu_nums "nic queues" $nic_queues
mask=$(echo "obase=16;2^$cpu_nums - 1" |bc)
flow_entries=$(echo "$nic_queues * 4096" |bc)
echo "mask = "$mask
echo "flow_entries = "$flow_entries
#for i in {0..$nic_queues}
while (($i < $nic_queues))
do
echo $mask > /sys/class/net/eth0/queues/rx-$i/rps_cpus
echo 4096 > /sys/class/net/eth0/queues/rx-$i/rps_flow_cnt
i=$(($i+1))
done
echo $flow_entries > /proc/sys/net/core/rps_sock_flow_entries
新建完成后执行以下命令:
chmod +x /usr/local/sbin/set_rps.sh
echo "/usr/local/sbin/set_rps.sh" >> /etc/rc.local
完成上述配置后,重启公网网关主机以使配置生效,并在无外网IP的子机上测试是否能够成功访问外网。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment