25

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?

6 Answers 6

20

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.

1
  • 7
    This example does not work: All traffic goes through the 'lo' interface as can be seen by using 'tcpdump'. You may also notice that both tap interfaces are still down. After bringing them up tcpdump works on the tap interfaces but shows not traffic at all on them. The Linux kernel uses it local routing table (see 'ip route show table local') to find out that these IPs are local and just routes the packets locally, without using 'vde_switch'. Having said all that, I have not yet found a way to make an socat connection through vde_switch and tap interfaces. Commented Nov 27, 2013 at 22:42
6

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).

3
  • 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
    Commented Jan 17, 2010 at 21:54
  • 2
    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
    Commented Jan 17, 2010 at 22:24
  • 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
    Commented Jan 17, 2010 at 22:25
1

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.

1

if you want your own subnet and don't want to bother to use vde.

look at this. In short:

# tunctl -t eth0
Set 'eth0' persistent and owned by uid 0
# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr a6:9b:fe:d8:d9:5e  
      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:500 
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Or with ip:

# ip tuntap add dev eth0 mode tap
# ip link ls dev eth0
  7: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
  link/ether 0e:55:9b:6f:57:6c brd ff:ff:ff:ff:ff:ff
0

man interfaces man ifconfig

just add a new stanza in /etc/network/interfaces

my example configuration:

iface eth0 inet static
   address 192.168.2.150
   netmask 255.255.255.0
   network 192.168.2.0
   broadcast 192.168.2.255
   gateway 192.168.2.253
   # dns-* options are implemented by the resolvconf package, if installed
   dns-nameservers 8.8.4.4


iface eth0:1 inet static
    address 192.168.2.2
    netmask 255.255.255.0
    network 192.168.2.0
    broadcast 192.168.2.255
    gateway 192.168.2.253
    # dns-* options are implemented by the resolvconf package, if installed
    dns-nameservers 8.8.4.4

--

eth0 has ip 192.168.2.150 while eth0:1 has 192.168.2.2

3
  • 9
    This does not create a new virtual device, it only configures an additional IP on an physical device.
    – Flow
    Commented Apr 11, 2012 at 14:44
  • 1
    @Flow what's the difference? Just curious? I mean in terms of consequences, not implementation. Commented Jun 14, 2014 at 11:34
  • 2
    There are many consequences. For example if you add a second IP to a connected physical interface, then the system will be reachable under this IP over that physical interface from external.
    – Flow
    Commented Jun 14, 2014 at 13:37
-1

You can use the vconfig command example:

vconfig add eth0 10 #virtual interface eth0.10 will be created

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.