Commit e6ff2612 authored by Martín Ferrari's avatar Martín Ferrari

Add use_pi flag to tap-related functions, to allow both modes of operation

parent 62e29870
...@@ -220,13 +220,13 @@ class TapNodeInterface(NSInterface): ...@@ -220,13 +220,13 @@ class TapNodeInterface(NSInterface):
"""Class to create a tap interface inside a name space, it """Class to create a tap 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_tap(iface) iface, self._fd = netns.iproute.create_tap(iface, 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)
......
...@@ -195,7 +195,7 @@ class address(object): ...@@ -195,7 +195,7 @@ class address(object):
h = (self.address.__hash__() ^ self.prefix_len.__hash__() ^ h = (self.address.__hash__() ^ self.prefix_len.__hash__() ^
self.family.__hash__()) self.family.__hash__())
return h return h
class ipv4address(address): class ipv4address(address):
def __init__(self, address, prefix_len, broadcast): def __init__(self, address, prefix_len, broadcast):
self.address = address self.address = address
...@@ -899,7 +899,7 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None, ...@@ -899,7 +899,7 @@ 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): def create_tap(iface, use_pi = False):
"""Creates a tap device and returns the associated file descriptor""" """Creates a tap device and returns the associated file descriptor"""
if isinstance(iface, str): if isinstance(iface, str):
iface = interface(name = iface) iface = interface(name = iface)
...@@ -907,11 +907,15 @@ def create_tap(iface): ...@@ -907,11 +907,15 @@ def create_tap(iface):
IFF_TAP = 0x0002 IFF_TAP = 0x0002
IFF_NO_PI = 0x1000 IFF_NO_PI = 0x1000
TUNSETIFF = 0x400454ca TUNSETIFF = 0x400454ca
mode = IFF_TAP | IFF_NO_PI mode = IFF_TAP
if not use_pi:
mode |= IFF_NO_PI
fd = os.open("/dev/net/tun", os.O_RDWR) fd = os.open("/dev/net/tun", os.O_RDWR)
if fd == -1: if fd == -1:
raise RuntimeError("Could not open /dev/net/tun") raise RuntimeError("Could not open /dev/net/tun")
err = fcntl.ioctl(fd, TUNSETIFF, struct.pack("16sH", iface.name, mode)) err = fcntl.ioctl(fd, TUNSETIFF, struct.pack("16sH", iface.name, mode))
if err < 0: if err < 0:
os.close(fd) os.close(fd)
......
...@@ -96,8 +96,8 @@ class Node(object): ...@@ -96,8 +96,8 @@ class Node(object):
setattr(i, k, v) setattr(i, k, v)
return i return i
def add_tap(self, **kwargs): def add_tap(self, use_pi = False, **kwargs):
i = netns.interface.TapNodeInterface(self) i = netns.interface.TapNodeInterface(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