TAP devices on OS X - how to

Discussions related to using VirtualBox on Mac OS X hosts.
Post Reply
hmijail
Posts: 8
Joined: 19. Feb 2009, 12:54

TAP devices on OS X - how to

Post by hmijail »

Long story short: I wanted the equivalent of VMware´s host-only networking in VirtualBox. That requires the creation of a virtual network interface (TAP interface), which was easy to do with earlier versions than VirtualBox 2.1.0. More recent versions than that got easier networking - but lost the capability to create TAP interfaces, counting on the user to do so if so she wishes.

Here I am documenting how to create TAP interfaces and using them in VirtualBox in OS X. I think in Linux the procedure is somewhat easier. My system is Mac OS X 10.5.6; that shouldn´t matter much.
  • We will be using the command line as superuser (root); that can be dangerous if you aren´t careful. You have been warned.
  • I am assuming that you already have a VM prepared to be run to which you want to add host-only networking. It must be shut down (not only paused); let´s hope that VirtualBox will get the capability of changing VM network options while running (like VMware does).
  • If you are lucky, the procedure is not complicated. If you aren´t, you might need some networking knowledge to fix whatever small problem appears (arp... routing?). Anyway, normal networking in the Mac should not be affected.
  1. Download and install the TUN/TAP drivers. You can do so from http://tuntaposx.sourceforge.net or with MacPorts.
  2. Prepare the TAP interface by typing this on a terminal window:

    Code: Select all

    sudo bash # we go superuser
    exec 4<>/dev/tap0  # opens device, creates interface tap0 
    ifconfig tap0 10.10.10.1 10.10.10.255 
    ifconfig tap0 up 
    hexdump -c <&4 # reads from device - a cheap etherdump 
    (Here, the tap0 interface is working, try ping 10.10.10.255 and see the packets appear... When you don´t want the TAP interface any longer, press control-C and continue) 
    exec 4>&-  # closes device, destroys interface tap0
    
  3. Now we want to set one of the VM´s network interfaces to Host Interface mode, and set it to be bridged against our new TAP interface. BUT, that can´t be done in the GUI; I'm assuming that that is a bug in v2.1.4 at least, since we can do so without any problem from the command line. That´s what we are going to do.

    Code: Select all

    VBoxManage list vms  # shows your VMs; get the name of the one to be modified (here, "MyVM")
    VBoxManage modifyvm MyVM -nic3 hostif  # set interface 3 to Host Interface mode
    VBoxManage modifyvm MyVM -hostifdev3 tap0:   #  and bridge it to our TAP
  4. Start the VM and configure the new network interface. In this example, the TAP interface in the Mac has been configured to use address 10.10.10.1; the VM could use address 10.10.10.2, for example. You will have to use manual configuration, since there is no DHCP server.
  5. With a bit of luck, you could already be done! Try pinging the Mac from the VM, and viceversa. For completeness, you could also try ssh'ing one from the other, or accesing some other service.
  6. If things still don´t work, shutting down the VM and VirtualBox and starting them again could help. You can try also closing the TAP interface and opening it again.
    But, if still something is wrong, in my setup I found a couple of strange problems that you could check for. The Mac had got a wrong ARP resolution for 10.10.10.2 (the VM´s IP); with arp -a I saw that both 10.10.10.1 and .2 were resolved as the MAC in the TAP interface. That can be easily solved with arp -d 10.10.10.2 (to force a new resolution), or even force the correct value with arp -s 10.10.10.2 AA:BB:CC:DD:EE:FF (with AA:BB:CC:DD:EE:FF being the MAC address of the 10.10.10.2 interface in the VM; you can get it with ifconfig if the guest is Linux or in the network properties in Windows).
Regarding the lack of a DHCP server: you could set up one, if you were so inclined... but I think you will also need then a classic DHCP client to set up the tap0 interface, since it seems to be created out of the control of OS X´s normal network facilities.

Another tip: a quick way to check that the virtual network is working is by using some Bonjour application. For example, "Bonjour Browser" in OS X will show services published by the VM (avahi/zeroconf in Linux). BUT, even if Bonjour is working, normal networking can still be failing because of arp issues!

Regards.
TerryE
Volunteer
Posts: 3572
Joined: 28. May 2008, 08:40
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: Ubuntu 10.04 & 11.10, both Svr&Wstn, Debian, CentOS
Contact:

Post by TerryE »

Thanks for this
Read the Forum Posting Guide
Google your Q site:VirtualBox.org or search for the answer before posting.
drosenstark
Posts: 23
Joined: 25. Nov 2008, 01:17

Re: TAP devices on OS X - how to

Post by drosenstark »

This is just what I've been looking for. So does the EtherDump have to be kept running as a command line? Isn't there a permanent way to install this thing?
drosenstark
Posts: 23
Joined: 25. Nov 2008, 01:17

Re: TAP devices on OS X - how to

Post by drosenstark »

I see, you don't need the EtherDump but you cannot exit the shell in which you tap0 up... any way to get this to "stick"?
drosenstark
Posts: 23
Joined: 25. Nov 2008, 01:17

Re: TAP devices on OS X - how to

Post by drosenstark »

THANKS, GOT IT ALL RUNNING. I include some additional instructions, but I cite this question itself

http://compileyouidontevenknowyou.blogs ... ox-on.html
hmijail
Posts: 8
Joined: 19. Feb 2009, 12:54

Re: TAP devices on OS X - how to

Post by hmijail »

Yes, you need the hexdump. (not etherdump! I was only making a comparison!).

To be exact: to keep the tap0 alive, you need to open the /dev/tap0 (done by the exec line), and keep it open (done by not closing it :wink: , which happens with the close line or indirectly by closing that bash instance).

BUT you not only want tap0 alive, you also want it to actually work. For that you will need to keep the internal buffer of the tap interface from getting full, and that is done by taking bits out of it. That is done with the hexdump, or with any other thing that will keep reading from the /dev/tap0 file descriptor (which is that 4). I guess you could simply dd to /dev/null, or tail -f , or whatever.

You could also inject bits into the tap interface by way of the same file descriptor, of course....... which would be a pretty interesting way to mess with that virtual network!
drosenstark
Posts: 23
Joined: 25. Nov 2008, 01:17

Re: TAP devices on OS X - how to

Post by drosenstark »

Right now it works, in that I can access my guest from host, even without the hex dump. Are you saying that it will only work for the first X bytes without hexdump?
drosenstark
Posts: 23
Joined: 25. Nov 2008, 01:17

Re: TAP devices on OS X - how to

Post by drosenstark »

@hmijall, first off, thanks so much for your original post and your response to my questions.

So as I understand it, without the hexdump the buffer will fill up and the tap adapter will stop working.

1) is that right? (sorry, this is a repeat from my prior question)
2) Will this command do, or am I doing something wrong (it seems to work):

hexdump -c 0<&4 > /dev/null &

3) If I just do an exit from the "sudo bash" command, isn't that enough to clean up the TAP adapter?

Thanks again for your continued help on this.

Best,
Daniel
hmijail
Posts: 8
Joined: 19. Feb 2009, 12:54

Re: TAP devices on OS X - how to

Post by hmijail »

drosenstark wrote:So as I understand it, without the hexdump the buffer will fill up and the tap adapter will stop working.

1) is that right? (sorry, this is a repeat from my prior question)
Yes. In fact, if you keep ping'ing long enough through the tap, eventually the ping will complain about something like "buffer full". (that was at least my limited experience: no hexdump -> eventual "buffer full")
drosenstark wrote:2) Will this command do, or am I doing something wrong (it seems to work):

hexdump -c 0<&4 > /dev/null &
I haven´t checked, but yes, it should work. But if you´re goint to just dump to null, I'd try something like dd of=/dev/null <&4 (so no resources wasted in the hex prettyprinting, however small).
drosenstark wrote:3) If I just do an exit from the "sudo bash" command, isn't that enough to clean up the TAP adapter?
If "clean up" means "make it disappear", yes, that should be enough. My second exec line was for cleanliness.

You´re welcome :)
drosenstark
Posts: 23
Joined: 25. Nov 2008, 01:17

Re: TAP devices on OS X - how to

Post by drosenstark »

Thanks so much for your complete answer. Here's my final script file (so I can stop working on this finally and get on with software dev)

Code: Select all

echo "starting"
exec 4<>/dev/tap0
ifconfig tap0 10.10.10.1 10.10.10.255
ifconfig tap0 up
ping -c1 10.10.10.1
echo "ending"
export PS1="tap interface>"
dd of=/dev/null <&4 & # continuously reads from buffer and dumps to null
and I run this using

Code: Select all

source tap.sh
Seems to work thus far.

Thanks!
drosenstark
Posts: 23
Joined: 25. Nov 2008, 01:17

Re: TAP devices on OS X - how to

Post by drosenstark »

Thanks God (well, Sun, Oracle, the VirtualBox team?) this is now a non-issue because 2.2.2 has virtual networks of all types. In OSX you just have to make sure to restart the host at some point!
Post Reply