Commit a77b99f3 authored by alina's avatar alina

Tun device interface added.

parent f4d2c774
......@@ -252,6 +252,33 @@ class TapNodeInterface(NSInterface):
except:
pass
class TunNodeInterface(NSInterface):
"""Class to create a tun interface inside a name space, it
can be connected to a Switch object with emulation of link
characteristics."""
def __init__(self, node):
"""Create a new tap interface. 'node' is the name space in which this
interface should be put."""
self._fd = None
self._slave = None
iface = netns.iproute.interface(name = self._gen_if_name())
iface, self._fd = netns.iproute.create_tun(iface)
netns.iproute.change_netns(iface.name, node.pid)
super(TunNodeInterface, self).__init__(node, iface.index)
@property
def fd(self):
return self._fd
def destroy(self):
if not self._fd:
return
debug("TunNodeInterface(0x%x).destroy()" % id(self))
try:
os.close(self._fd)
except:
pass
class ExternalInterface(Interface):
"""Add user-facing methods for interfaces that run in the main
namespace."""
......
......@@ -929,3 +929,28 @@ def create_tap(iface, use_pi = False):
interfaces = get_if_data()[1]
return interfaces[iface.name], fd
def create_tun(iface):
"""Creates a tun device and returns the associated file descriptor"""
if isinstance(iface, str):
iface = interface(name = iface)
assert iface.name
IFF_TUN = 0x0001
TUNSETIFF = 0x400454ca
mode = IFF_TUN
fd = os.open("/dev/net/tun", os.O_RDWR)
err = fcntl.ioctl(fd, TUNSETIFF, struct.pack("16sH", iface.name, mode))
if err < 0:
os.close(fd)
raise RuntimeError("Could not configure device %s" % iface.name)
try:
set_if(iface)
except:
os.close(fd)
raise
interfaces = get_if_data()[1]
return interfaces[iface.name], fd
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment