views:

745

answers:

3

I am testing an implementation of a protocol that talks between two computers using ethernet (not IP). In order to not actually have to have two physical computers, I want to create two virtual ethernet interfaces. These would only be able to talk to each other, so one endpoint program would bind to one interface and the other endpoint would bind to the other.

Is this possible and how do I do it?

+2  A: 

You can use the "tap" virtual ethernet driver which lets a userspace program pretend to be an ethernet interface. This is a standard kernel feature for some time now (it might not be enabled in your kernel though).

MarkR
The module is called tun. But yes, otherwise, this answer is good. Just do a insmod tun, otherwise compile the kernel with support for it.
Anders
The module is called tun, it provides the "tap" device as well. See the kernel's Documentation/networking/tuntap.txt for the full userspace interface; virtual interfaces are created by using an ioctl on a character device /dev/net/tun
MarkR
tun0 is a virtual point-to-point device, tap0 is a virtual ethernet. The latter can be used with any protocol, not just IP
MarkR
+5  A: 

You can use VDE2, a virtual switch.

For example (you will need a few terms):

# Install vde2 (assumes Debian/Ubuntu)
sudo aptitude install vde2
# Create the switch and two tap interfaces attached to it
sudo vde_switch -tap tap0 -tap tap1
# Configure the interfaces
sudo ip addr add 10.0.31.10 dev tap0
sudo ip addr add 10.0.31.11 dev tap1
# Start a server
socat - TCP-LISTEN:4234,bind=10.0.31.10
# Alternatively, an echo server:
#socat PIPE TCP-LISTEN:4234,bind=10.0.31.10
# Start a client
socat - TCP:10.0.31.10:4234,bind=10.0.31.11

Type on one side, it will appear on the other.

Tobu
This is simple and effective for what I need, thanks
Greg Rogers
+1  A: 

You can use ns3 to emulate a complicated network between two tap devices if you need it: http://www.nsnam.org/

I've had it emulating two switches, a wireless client, and an AP, between two virtualbox instances.

Andrew McGregor