分类: LINUX
2011-02-14 12:10:48
几年前写的关于用Linux Netemu模块来模拟网络丢包和时延的文档,以前是用来测试一些WAN加速器设备和软件的。如果有朋友需要,我可以转成中文的
==========================================================================================================
1. Purpose.
Sometimes we want to emulate/simulate the network traffic such as latency, package loss ratio for testing, but it is not easy to get some dedicated emulator/simulator equipment.
So this document describes the basic steps how to setup own emulator just based on generic Linux system.
By default, if you get more than one NIC, the Linux machine can act as a Router when you put it into your existing network. But for testing, normally we don’t want to change any existing environment configuration. So it’s better to put the emulator into our network as transparent equipment.
So in this document, we are going to setup a transparent bridge with network emulator function.
2. Detail.
(1) Hardware,
Need one machine (PC or laptop) with at least two NICs.
The NICs speed mode should accords with your real test environment.
(2) Software,
The network emulation function is provided by ‘Netem’ component.
If you install some latest version Linux(such as CentOS5), this component will be already built in Kernel or loaded as model.
If your Linux kernel does not enable the Netem, you should re-build the Kernel, and enable it from
Networking -->
Networking Options -->
Qos and/or fair queuing --> Network emulator
Notes: How to check if Netem is enabled in your Kernel: You can just type the command: # tc qdisc add dev If no error, than Netem is enabled. If you get some error such as “RTNETLINK answers: No such file or directory” Normally, Netem is configured as model by default, so you can use ‘lsmod|grep netem” to check if it is loaded. |
Bridge function is enabled in most released versions of Linux.
If not, you should re-build your kernel to enable it from:
Networking -->
Networking options-->802.1d Ethernet Bridgeing
You also need ‘bridge-utils’ package to manage the bridge function, if none in your system; you should download and install it.
After bridge enabled and bridge-utils installed, you can use following steps to verify.
# brctl show bridge name bridge id STP enabled interfaces # lsmod | grep bridge bridge 53341 0 |
3. Testing steps
Now, the emulator is ready. We can put it into your network and configure it.
Before that, we can just use two PCs to test if the functions work.
* Notes, for we use transparent bridge mode, two PCs are assigned the same subnet IP addresses.
1) Create the logical bridge
Use ‘brctl addbr # brctl addbr testbridge
After that, you can find a logical interface via ‘ifconfig’
# ifconfig testbridge testbridge Link encap:Ethernet HWaddr 00:00:00:00:00:00 BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) |
2) Add the network interfaces into the logical bridge.
Use “brctl addif # brctl addif testbridge eth0 # brctl addif testbridge eth1
*Notice, If you had assigned the IP address to the interface, after it is added into bridge, IP address of this physical interface will be lost and all connection through this interface will be also lost.
3) Enable the bridge.
Enable the logical bridge by ‘ifconfig # ifconfig testbridge up
4) Test bridge
Now you can ‘ping’ from one PC to another PC.
If ping succeeds, the bridge works!
5) Add delay to bridge
Now we can add 100ms delay to the bridge via ‘tc’ command.
# tc qdisc add dev eth0 root netem delay 100ms |
And than use ping to check if delay happens.
6) Add package loss to bridge
Than emulate 10% package loss,
# tc qdisc change dev eth0 root netem loss 10% |
Use ping to check if package loss happens.
7) Add both delay and package loss to bridge
Seems I can’t add both two situations to one interface at the same time, so we need to add each situation to different interfaces.
# tc qdisc del dev eth0 # tc qdisc del dev eth1 # tc qdisc add dev eth1 root netem loss 10% |
Use ping to verify it.
8) Now you can use emulator for formal test. Just plug it into where you want. For it is transparent, you do not need to change any IP and Routing configuration.
4. Tips
Here is some Tips for easy using:
1) tc command is fair long, we can use alias or write some simply script to simplify the parameters.
2) By default, after bridge enabled, neither physical interface nor logical bridge will have IP address. We can use ‘ifconfig’ to assign a IP to bridge so that the bridge can be accessed remotely.
Ex. # ifconfig testbridge 192.168.1.1 netmask 255.255.255.0
Notes: Do not assign IP address to network interface after bridge enabled.
3) You can also write all commands about bridge configuration into a script, and put it into /etc/init.d and /etc/rcx.d, so that the bridge function can be enabled during system booting.
4) Further, you can also add more NICs into emulator; it can be acted as a real SWITCH, and support some feature like STP.
5) The network delay and package loss are most useful for our testing, but Netem can also support other more feature, such as package duplication and re-ordering. Please refer the reference document.
5. Reference
Net:Netem – The Linux Foundation
Net:Bridge – The Linux Foundation