Commit 5fab780a authored by alina's avatar alina

small changes in tun/tap interface creation

parent 9b8ee6f2
...@@ -235,7 +235,7 @@ class TapNodeInterface(NSInterface): ...@@ -235,7 +235,7 @@ class TapNodeInterface(NSInterface):
self._fd = None self._fd = None
self._slave = None self._slave = None
iface = netns.iproute.interface(name = self._gen_if_name()) iface = netns.iproute.interface(name = self._gen_if_name())
iface, self._fd = netns.iproute.create_tap(iface, use_pi) iface, self._fd = netns.iproute.create_tap(iface, use_pi = use_pi)
netns.iproute.change_netns(iface.name, node.pid) netns.iproute.change_netns(iface.name, node.pid)
super(TapNodeInterface, self).__init__(node, iface.index) super(TapNodeInterface, self).__init__(node, iface.index)
...@@ -256,13 +256,14 @@ class TunNodeInterface(NSInterface): ...@@ -256,13 +256,14 @@ class TunNodeInterface(NSInterface):
"""Class to create a tun interface inside a name space, it """Class to create a tun interface inside a name space, it
can be connected to a Switch object with emulation of link can be connected to a Switch object with emulation of link
characteristics.""" characteristics."""
def __init__(self, node): def __init__(self, node, use_pi = False):
"""Create a new tap interface. 'node' is the name space in which this """Create a new tap interface. 'node' is the name space in which this
interface should be put.""" interface should be put."""
self._fd = None self._fd = None
self._slave = None self._slave = None
iface = netns.iproute.interface(name = self._gen_if_name()) iface = netns.iproute.interface(name = self._gen_if_name())
iface, self._fd = netns.iproute.create_tun(iface) iface, self._fd = netns.iproute.create_tap(iface, use_pi = use_pi,
tun = True)
netns.iproute.change_netns(iface.name, node.pid) netns.iproute.change_netns(iface.name, node.pid)
super(TunNodeInterface, self).__init__(node, iface.index) super(TunNodeInterface, self).__init__(node, iface.index)
......
...@@ -901,16 +901,20 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None, ...@@ -901,16 +901,20 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
for c in commands: for c in commands:
execute(c) execute(c)
def create_tap(iface, use_pi = False): def create_tap(iface, use_pi = False, tun = False):
"""Creates a tap device and returns the associated file descriptor""" """Creates a tap/tun device and returns the associated file descriptor"""
if isinstance(iface, str): if isinstance(iface, str):
iface = interface(name = iface) iface = interface(name = iface)
assert iface.name assert iface.name
IFF_TUN = 0x0001
IFF_TAP = 0x0002 IFF_TAP = 0x0002
IFF_NO_PI = 0x1000 IFF_NO_PI = 0x1000
TUNSETIFF = 0x400454ca TUNSETIFF = 0x400454ca
mode = IFF_TAP if tun:
mode = IFF_TUN
else:
mode = IFF_TAP
if not use_pi: if not use_pi:
mode |= IFF_NO_PI mode |= IFF_NO_PI
...@@ -929,28 +933,3 @@ def create_tap(iface, use_pi = False): ...@@ -929,28 +933,3 @@ def create_tap(iface, use_pi = False):
interfaces = get_if_data()[1] interfaces = get_if_data()[1]
return interfaces[iface.name], fd 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
...@@ -116,8 +116,8 @@ class Node(object): ...@@ -116,8 +116,8 @@ class Node(object):
setattr(i, k, v) setattr(i, k, v)
return i return i
def add_tun(self, **kwargs): def add_tun(self, use_pi = False, **kwargs):
i = netns.interface.TunNodeInterface(self) i = netns.interface.TunNodeInterface(self, use_pi)
for k, v in kwargs.items(): for k, v in kwargs.items():
setattr(i, k, v) setattr(i, k, v)
return i return i
......
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