Tun Tap and Veth for Fun

shashank Jain
3 min readMay 26, 2018

--

In virtualized world there is a need to send packets across from virtual machines to the actual physical devices. This needs a mechanism to have virtualized devices in the hypervisor. Linux provides a mechanism to create virtual network devices called tun and tap . The tun device acts at layer 3 of the network stack which means it receives the IP packets where as tap device acts at layer 2 where it receives raw ethernet packets.

Now one might ask, what is the use for these devices. We take a scenario like openstack where in the VM needs to send packets outbound to other VM on a different compute node or over internet. The packets from VM are transmitted to the hypervisor which smartly uses a tap device and using this tap device passes the packet to a software bridge. The bridge then can be connected to other bridges as in case of openstack like (br-int and br-tun bridges) or directly to a physical device which is also connected to the software bridge.

Lets see how tap devices work with a simple example

Here I create 2 tap devices mytap1 and mytap2

Listing the tap devices we can see there are two network interfaces created.

We assign IP addresses to the devices.

Doing a simple ping from one device to other

In the above examples we explicitly created two tap devices and tried a ping between the two.

We can also use veth pairs which can be thought of as a virtual cable to connect the virtual devices. Used in openstack to connect the software bridges.

Creating a veth pair

This creates 2 tap interfaces firstap and secondtap.

Adding IP addresses to the tap devices and trying a ping

In next section we cover how to create a tun device programatically.

Disclaimer : The views expressed above are personal and not of the company I work for.

--

--