Friday 27 March 2015

How to Run IPerf Traffic on same Computer with two Interfaces


Setup:

           Linux PC/Device -----------Eth1--------|
                                  |                            | Loopback cable
                                   |-------------Eth2------- |

Requirements:

  The Iperf traffic should pass externally from the Ethernet interfaces which are connected using Ethernet cable..

Problem:

We have one interface which is called as loopback interface (lo). When we ping or send traffic to test local interface it is the loopback interface which replies.

Lets say we have three interfaces on Linux PC eth1, eth2 and lo (loopback interface). 

Commands

Ifconfig eth1 10.1.1.1 netmask 255.255.255.0 up
ifconfig eth2 10.2.1.1 netmask 255.255.255.0 up

ifconfig -> Verify loopback interface is up

ping 10.1.1.1 -> Reply will come

ping 10.2.1.1 -> Reply will come

Now disable loopback interface

ifconfig lo down

ping 10.1.1.1 -> Reply will not come

ping 10.2.1.1 -> Reply will not come

So the problem is if the loopback interface is present this interface will reply and the packets will not go from out side the cable or in other words the kernel detects that the destination is a local one, so the traffic is looped back to the machine itself without going through eth1 or eth2.


Solution

Got solution using NAT iptables rules as described in below reference link.


ifconfig eth0 10.50.0.1 netmask 255.255.255.0
ifconfig eth1 10.50.1.1 netmask 255.255.255.0
iptables -t nat -L
iptables -t nat -A POSTROUTING -s 10.50.0.1 -d 10.60.1.1 -j SNAT --to-source 10.60.0.1
iptables -t nat -A PREROUTING -d 10.60.0.1 -j DNAT --to-destination 10.50.0.1
iptables -t nat -A POSTROUTING -s 10.50.1.1 -d 10.60.0.1 -j SNAT --to-source 10.60.1.1
iptables -t nat -A PREROUTING -d 10.60.1.1 -j DNAT --to-destination 10.50.1.1
ip route add 10.60.1.1 dev eth0
arp -i eth0 -s 10.60.1.1 00:22:45:f1:18:53 # eth1's mac address
ip route add 10.60.0.1 dev eth1
arp -i eth1 -s 10.60.0.1 02:22:23:f1:18:52 # eth0's mac address
ping 10.60.1.1

Using above commands it was possible to force the traffic outside the cable.

Once setup is ready, run the Iperf server and client on the PC.

# server
iperf -B 10.50.0.1 -s -u -w 256k -l 1KB &
# client
iperf -B 10.50.1.1 -c 10.60.0.1 -u -b 600M -w 256k -l 1KB -P 10 -t 60