Building a WAN simulator

Building a WAN latency generator is much easier than you think, particularly since the Linux kernel has netem built into the kernel

Here are some notes on the config needed to make it work

Install the latest command line instance of CentOS on a box that has two NICs in it

Bring it bang up to date – reboot if this increases the version for completeness
(check /etc/centos-release)

yum update yum
yum clean all
yum update

The key to making it work is to make the two Ethernet cards into a bridge, onto which you can put a config including an IP address. In this way the interfaces work as an in and an out and also as the interface that can be used to manage the machine.

Here are the contents of the key network files

/etc/resolv.conf
nameserver=192.168.1.1

/etc/sysconfig/network
NETWORKING=yes
HOSTNAME=hostname.domain.com
GATEWAY=192.168.1.1

/etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE="eth1"
HWADDR="00:0E:A6:28:01:07"
ONBOOT="yes"
BRIDGE="br0"

/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="none"
HWADDR="00:50:BA:26:E5:71"
ONBOOT="yes"
BRIDGE="br0"

/etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
TYPE="Bridge"
IPADDR="192.168.1.97"
NETMASK="255.255.255.0"
ONBOOT="Yes"
BOOTPROTO="None"
DELAY=5

We also need to have packet forwarding switched on. To do this we need to edit the /etc/sysctl.conf file

# Controls IP packet forwarding
net.ipv4.ip_forward = 1

Enable sshd (allows you to use headless)

service sshd start
chkconfig sshd on

If you want to persist sessions then install screen

 yum install screen

(screen -h 10000 gets you a new screen with a 10000 line buffer. Once inside use ^a ? to get a list of options. ^a d to detach, screen -list to list open screens, screen -r 1346 to rejoin screen 1346)

Once you’ve done all this config at a console, a reboot is in order. You should be able to ssh onto the box on the IP address in the bridge settings.

Note – you may need to turn off the Centos Network manager and enable network at boot time

chkconfig NetworkManager off
chkconfig –levels 35 network on
/etc/init.d/NetworkManager stop
/etc/init.d/network restart

Now we can add use some commands to add latency and packet loss to the interfaces to simulate our WAN. This command introduces a 100ms delay on eth1
(If you need to work out which interface is the1 and which is eth0, add some delay to one of them and then ping the gateway. You should be able to see the delay, if not add some to the other and use the ping to confirm…)

tc qdisc add dev eth1 root netem delay 100ms

For our client, if we build a delay on both the inbound and the ourbound, we will delay packets in bot directions before it gets to our client ….

tc qdisc change dev eth0 root netem delay 100ms 10ms \
distribution normal
tc qdisc change dev eth1 root netem delay 100ms 10ms \
distribution normal

This causes a 100ms +/- 10ms delay (note change used in this case as the 4th parm – fails if you did not use add first – or delete first). The distribution normal command causes a randomisation of the delay.

How do I turn it off I here you ask …

tc qdisc del dev eth1 root

You should now be able to connect a computer on the spare interface.

The following is a full set of commands to implement delay ….note we are setting up the throttling first then adding the delay

tc qdisc replace dev eth0 root handle 1:0 \
 tbf rate 1Mbit burst 2048 latency 100ms
tc qdisc replace dev eth1 root handle 1:0 \
 tbf rate 1Mbit burst 2048 latency 100ms
tc qdisc add dev eth0 parent 1:1 handle 10: \
 netem delay 150ms 10ms distribution normal
tc qdisc add dev eth1 parent 1:1 handle 10: \
 netem delay 150ms 10ms distribution normal

and the reset

 tc qdisc del dev eth0 root
 tc qdisc del dev eth1 root

It will be able to DHCP through the bridge, with the appropriate WAN effects …

And that’s all there is to it folks

Here are a few useful links expanding on the subject …

Details of how to use tc to shape bandwidth

Use of screen

http://tdistler.com/2011/06/10/netem-wan-emulation-how-to-setup-a-netem-box

http://www.banym.de/linux/centos/setup-bridge-device-on-centos

 

Advertisements

One thought on “Building a WAN simulator”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s