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

Fix destroy() issues. Add import_if()

parent 564e6000
...@@ -14,15 +14,16 @@ class Node(object): ...@@ -14,15 +14,16 @@ class Node(object):
s = sorted(Node._nodes.items(), key = lambda x: x[0]) s = sorted(Node._nodes.items(), key = lambda x: x[0])
return [x[1] for x in s] return [x[1] for x in s]
def __init__(self, debug = False, nonetns = False): def __init__(self, debug = 0, nonetns = False):
"""Create a new node in the emulation. Implemented as a separate """Create a new node in the emulation. Implemented as a separate
process in a new network name space. Requires root privileges to run. process in a new network name space. Requires root privileges to run.
If keepns is true, the network name space is not created and can be run If keepns is true, the network name space is not created and can be run
as a normal user, for testing. If debug is true, details of the as a normal user, for testing. If debug is true, details of the
communication protocol are printed on stderr.""" communication protocol are printed on stderr."""
# Initialize attributes, in case something fails during __init__ # Initialize attributes, in case something fails during __init__
self._pid = self._debug = self._slave = None self._pid = self._slave = None
self._processes = weakref.WeakValueDictionary() self._processes = weakref.WeakValueDictionary()
self._interfaces = weakref.WeakValueDictionary() self._interfaces = weakref.WeakValueDictionary()
...@@ -42,18 +43,19 @@ class Node(object): ...@@ -42,18 +43,19 @@ class Node(object):
def destroy(self): def destroy(self):
if self._debug: # pragma: no cover if self._debug: # pragma: no cover
sys.stderr.write("*** Node(%s) destroy\n" % self.pid) sys.stderr.write("*** Node(%s) destroy\n" % self.pid)
for p in self._processes.values(): for p in self._processes.values():
p.destroy() p.destroy()
self._processes.clear()
# Use get_interfaces to force a rescan # Use get_interfaces to force a rescan
for i in self.get_interfaces(): for i in self.get_interfaces():
i.destroy() i.destroy()
self._interfaces.clear()
if self._slave: if self._slave:
self._slave.shutdown() self._slave.shutdown()
self._pid = self._slave = None
del self._processes
del self._interfaces
del self._pid
del self._slave
@property @property
def pid(self): def pid(self):
...@@ -88,6 +90,9 @@ class Node(object): ...@@ -88,6 +90,9 @@ class Node(object):
setattr(i, k, v) setattr(i, k, v)
return i return i
def import_if(self, interface):
return netns.interface.ImportedNodeInterface(self, interface)
def del_if(self, iface): def del_if(self, iface):
"""Doesn't destroy the interface if it wasn't created by us.""" """Doesn't destroy the interface if it wasn't created by us."""
del self._interfaces[iface.index] del self._interfaces[iface.index]
...@@ -107,7 +112,8 @@ class Node(object): ...@@ -107,7 +112,8 @@ class Node(object):
for i in list(self._interfaces): # copy before deleting! for i in list(self._interfaces): # copy before deleting!
if i not in ifaces: if i not in ifaces:
if self._debug: if self._debug:
sys.stderr.write("WARNING: interface #%d went away." % i) sys.stderr.write("WARNING: interface #%d went away.\n" % i)
self._interfaces[i].destroy()
del self._interfaces[i] del self._interfaces[i]
return sorted(ret, key = lambda x: x.index) return sorted(ret, key = lambda x: x.index)
......
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