构建一台千兆级的NAT服务器

27 views
Skip to first unread message

netkiller

unread,
Dec 11, 2009, 9:31:54 PM12/11/09
to netkiller
构建一台千兆级的NAT服务器
Ippen Yang
(ip...@21cn.com)

1.前言
本文档主要描述如何利用Linux构建一台能力强大的NAT服务器,并发连接数高达50万或更高(最高曾经有70万),流量1GB,系统稳定。这个方案
适合动手能力强的朋友。

2.许可协议
本文的许可协议遵循GNU Free Document License。协议的具体内容请参见http://www.gnu.org/
copyleft/fdl.html。在遵循GNU Free Document License的基础上,可以自由地转载或发行本文,但请保留本文的
完整性。

3.准备工作
CentOS 4.6光盘一套(4张CD)
kernel-2.6.28.9内核源码
PC服务器一台,4核 CPU,速度越快越好,2G或以上的硬盘(可以用CF电子盘),两个高性能PCI-E千兆网卡(一定要Intel PCI-E的
网卡),
参考硬件配置:Intel S3200,512M内存*2,Q6600 CPU,80G硬盘,Intel千兆网卡

4.安装
首先安装CentOS,安装时记住要安装开发工具,因为要编译内核;安装时不要建立交换区,切记!!
安装完成后,运行setup,在system service中将不需要用的程序全部停止,基本上只需要保留ssh,iptables,
network,snmpd,syslog几个,设置完后重新启动服务器。
将kernel文件解压到/usr/src中,然后运行 make menuconfig,配置内核编译参数,基本上不用改什么参数,注意要选择多
CPU,选择netfilter中的参数,指定你需要编译的模块,然后执行make编译内核,make modules_install 安装内核模
块,make install 安装内核
编辑/etc/grub.conf

QUOTE:
#boot=/dev/sda1
default=0
timeout=1
#splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.28.9)
root (hd0,2)
kernel /boot/vmlinuz-2.6.28.9 ro root=LABEL=/
initrd /boot/initrd-2.6.28.9.img
title CentOS-4 i386 (2.6.9-34.ELsmp)
root (hd0,0)
kernel /boot/vmlinuz-2.6.9-34.ELsmp ro root=LABEL=/
initrd /boot/initrd-2.6.9-34.ELsmp.img
title CentOS-4 i386-up (2.6.9-34.EL)
root (hd0,0)
kernel /boot/vmlinuz-2.6.9-34.EL ro root=LABEL=/
initrd /boot/initrd-2.6.9-34.EL.img

将服务器调整使用2.6.28.9内核启动
编辑/etc/modprobe.conf
确认PCI-E网卡的模块正常,CentOS 4.6有时候将PCI-E的网卡当pci网卡认,PCI-E的网卡驱动是e1000e
编辑/etc/sysctl.conf,查找行“net.ipv4.ip_forward = 0”,然后将这行按下面的内容进行更改,原文件中没有的
内容请自己添加

QUOTE:
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
net.ipv4.netfilter.ip_conntrack_max = 1048576
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 300

编辑/etc/rc.local如下:

QUOTE:
#!/bin/sh
touch /var/lock/subsys/local
/root/nat

编辑/root/nat如下:

QUOTE:
# eth0 is internet interface
# eth1 is intranet interface
INCOMING=eth0
OUTGOING=eth1
ip addr add 222.222.222.8 dev $OUTGOING
ip addr add 222.222.222.9 dev $OUTGOING
ip addr add 222.222.222.10 dev $OUTGOING
ip addr add 222.222.222.11 dev $OUTGOING
IPPOOL=222.222.222.8-222.222.222.11
iptables -t nat -F
iptables -t nat -A POSTROUTING -s 内部IP地址/子网 -o $OUTGOING -j SNAT --to
$IPPOOL
iptables-save -c >/etc/sysconfig/iptables

注意,这里假定互联网IP是222.222.222.8-222.222.222.11(上面黑色字体),请用真正使用的IP范围将黑色字体替换。内网
地址也请根据实际环境变更。这个脚本只是NAT用,如果需要增加安全策略,请参考其它的iptables的脚本。
完成后,重启服务器
重启后,登录到系统中
察看中断分配情况
cat /proc/interrupts

QUOTE:
CPU0 CPU1 CPU2 CPU3
0: 27 0 0 0 IO-APIC-edge
timer
1: 0 1 1 0 IO-APIC-edge
i8042
6: 0 1 0 1 IO-APIC-edge
floppy
9: 0 0 0 0 IO-APIC-fasteoi
acpi
12: 1 0 1 2 IO-APIC-edge
i8042
14: 0 0 0 0 IO-APIC-edge
ide0
15: 0 0 0 0 IO-APIC-edge
ide1
17: 0 0 0 0 IO-APIC-fasteoi
uhci_hcd:usb3, ehci_hcd:usb7
18: 0 0 0 0 IO-APIC-fasteoi
uhci_hcd:usb1, uhci_hcd:usb6
19: 0 0 0 0 IO-APIC-fasteoi
uhci_hcd:usb5
21: 0 0 0 0 IO-APIC-fasteoi
uhci_hcd:usb2
23: 0 0 0 0 IO-APIC-fasteoi
uhci_hcd:usb4, ehci_hcd:usb8
379: 180564744 180564148 6 4 PCI-MSI-edge
eth1
380: 5 3 181325020 181321112 PCI-MSI-edge
eth0
381: 528 539 188 188 PCI-MSI-edge
ahci
NMI: 0 0 0 0 Non-maskable
interrupts
LOC: 2098128 2098112 2098094 2098076 Local timer
interrupts
RES: 1235 1318 1707 2126 Rescheduling
interrupts
CAL: 1418 1424 1423 10 Function call
interrupts
TLB: 260 245 639 597 TLB shootdowns
TRM: 0 0 0 0 Thermal event
interrupts
SPU: 0 0 0 0 Spurious interrupts
ERR: 0
MIS: 0

找当中与网卡有关的中断,上面的表中,中断379对应eth1,中断380对应eth0
进入对应的中断控制目录,指定cpu0 和cpu1负责eth1的中断处理
cd /proc/irq/379
echo 3 >smp_affinity
指定cpu2和cpu3负责eth0的中断处理
cd /proc/irq/380
echo c >smp_affinity
这样,指定cpu响应不同网卡的请求,提高系统处理能力(上面的表中能明显看出网卡的中断是分配在不同的cpu上的)
补充:
修改tcp_timeout_established的参数,在文件 net/netfilter/
nf_conntrack_proto_tcp.c 中
修改buckets和ip_conntrack_max的参数,在文件 net/netfilter/nf_conntrack_core.c 里

需要修改参数请在编译前修改
不明白请参考我原来写的文章 http://linux.chinaunix.net/bbs/viewthread.php?tid=1015371&extra=page%3D2%26amp%3Bfilter%3Ddigest
有问题请上CU的论坛(http://bbs.chinaunix.net)发帖子。

Reply all
Reply to author
Forward
0 new messages