user1901077
user1901077

Reputation: 19

How do I hide a network interface from a process under Linux?

I am trying to do some network testing with a 10G network card which has 2 ports (eth1, eth2). In order to test, I would use something like iperf to do bandwidth testing:

I connect a cable directly from port 1(eth1) to port 2(eth2).

ip addresses:

eth1: 192.168.20.1/24
eth2: 192.168.20.2/24

Terminal 1:

user@host:~$ iperf -s -B 192.168.20.1

Terminal 2:

user@host:~$ iperf -c 192.168.20.1

Results:

------------------------------------------------------------
Client connecting to 192.168.20.1, TCP port 5001
TCP window size:  169 KByte (default)
------------------------------------------------------------
[  3] local 192.168.20.1 port 53293 connected with 192.168.20.1 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec  41.6 GBytes  35.7 Gbits/sec

As you can see, the data is not going through the cable at all but just through the localhost or memory which is why I am getting speeds above 10G.

Is it possible to hide eth1 from the command "iperf -c 192.168.20.1" so that the data is forced through the cable?

Update 1:

I have now tried the following after a reference made by @Mardanian :

Note: Ports are now eth2/eth3 (not eth1/eth2)
eth2 has mac address 00:40:c7:6c:01:12
eth3 has mac address 00:40:c7:6c:01:13

ifconfig eth2 192.168.20.1/24 up
ifconfig eth3 192.168.20.2/24 up
arp -s 192.168.20.3 00:40:c7:6c:01:12
arp -s 192.168.20.4 00:40:c7:6c:01:13
ip route add 192.168.20.3 dev eth3
ip route add 192.168.20.4 dev eth2
iptables -t nat -A POSTROUTING -d 192.168.20.4 -j SNAT --to-source 192.168.20.3
iptables -t nat -A POSTROUTING -d 192.168.20.3 -j SNAT --to-source 192.168.20.4
iptables -t nat -A PREROUTING -d 192.168.20.3 -j DNAT --to-destination 192.168.20.1
iptables -t nat -A PREROUTING -d 192.168.20.4 -j DNAT --to-destination 192.168.20.2

iperf -s -B 192.168.20.3
bind failed: Cannot assign requested address

These dummy addresses do not seem to work properly, I can't seem to bind or even ping them.

arp -an
? (192.168.20.3) at 00:40:c7:6c:01:12 [ether] PERM on eth2
? (192.168.20.4) at 00:40:c7:6c:01:13 [ether] PERM on eth3

As far as I understand, arp doesn't bind an ip address to an interface, it just tells the system that in order find a certain ip, it lets the system know which interface to go through - that is why I cannot bind to the dummy ip addresses. If I bind to the real ip addresses, then I still would be going through the local system.

Upvotes: 1

Views: 3358

Answers (1)

Mardanian
Mardanian

Reputation: 191

iperf will always use loopback if it detects the destination is local. Force kernel to route it through inteface. see linux: disable using loopback and send data via wire between 2 eth cards of one comp

Upvotes: 1

Related Questions