Properly release UDP ports when closing a project or deleting a link.

This commit is contained in:
Jeremy committed 2015-02-23 19:00:34 -07:00
1 parent 3d3300e83a
commit 42c07cee1a
16 files changed
+96 -210

No files matched your search

-7
View File
@@ -46,7 +46,6 @@ from .dynamips_device import DynamipsDevice
# NIOs
from .nios.nio_udp import NIOUDP
from .nios.nio_udp_auto import NIOUDPAuto
from .nios.nio_unix import NIOUNIX
from .nios.nio_vde import NIOVDE
from .nios.nio_tap import NIOTAP
@@ -355,13 +354,7 @@ class Dynamips(BaseManager):
sock.connect((rhost, rport))
except OSError as e:
raise DynamipsError("Could not create an UDP connection to {}:{}: {}".format(rhost, rport, e))
# check if we have an allocated NIO UDP auto
#nio = node.hypervisor.get_nio_udp_auto(lport)
# if not nio:
# otherwise create an NIO UDP
nio = NIOUDP(node.hypervisor, lport, rhost, rport)
# else:
# nio.connect(rhost, rport)
elif nio_settings["type"] == "nio_generic_ethernet":
ethernet_device = nio_settings["ethernet_device"]
if sys.platform.startswith("win"):
@@ -25,7 +25,6 @@ import logging
import asyncio
from .dynamips_error import DynamipsError
from .nios.nio_udp_auto import NIOUDPAuto
log = logging.getLogger(__name__)
@@ -53,7 +52,6 @@ class DynamipsHypervisor:
self._devices = []
self._working_dir = working_dir
self._nio_udp_auto_instances = {}
self._version = "N/A"
self._timeout = timeout
self._uuid = None
@@ -130,7 +128,6 @@ class DynamipsHypervisor:
except OSError as e:
log.debug("Stopping hypervisor {}:{} {}".format(self._host, self._port, e))
self._reader = self._writer = None
self._nio_udp_auto_instances.clear()
@asyncio.coroutine
def reset(self):
@@ -139,7 +136,6 @@ class DynamipsHypervisor:
"""
yield from self.send("hypervisor reset")
self._nio_udp_auto_instances.clear()
@asyncio.coroutine
def set_working_dir(self, working_dir):
@@ -224,31 +220,6 @@ class DynamipsHypervisor:
self._host = host
def get_nio_udp_auto(self, port):
"""
Returns an allocated NIO UDP auto instance.
:returns: NIO UDP auto instance
"""
if port in self._nio_udp_auto_instances:
return self._nio_udp_auto_instances.pop(port)
else:
return None
def allocate_udp_port(self):
"""
Allocates a new UDP port for creating an UDP NIO Auto.
:returns: port number (integer)
"""
# use Dynamips's NIO UDP auto back-end.
nio = NIOUDPAuto(self, self._host, self._udp_start_port_range, self._udp_end_port_range)
self._nio_udp_auto_instances[nio.lport] = nio
allocated_port = nio.lport
return allocated_port
@asyncio.coroutine
def send(self, command):
"""
@@ -1,140 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Interface for automatic UDP NIOs.
"""
import asyncio
from .nio import NIO
import logging
log = logging.getLogger(__name__)
class NIOUDPAuto(NIO):
"""
Dynamips auto UDP NIO.
:param hypervisor: Dynamips hypervisor instance
:param laddr: local address
:param lport_start: start local port range
:param lport_end: end local port range
"""
_instance_count = 0
def __init__(self, hypervisor, laddr, lport_start, lport_end):
# create an unique ID and name
nio_id = NIOUDPAuto._instance_count
NIOUDPAuto._instance_count += 1
name = 'nio_udp_auto' + str(nio_id)
self._laddr = laddr
self._lport = None
self._raddr = None
self._rport = None
NIO.__init__(self, name, hypervisor)
@classmethod
def reset(cls):
"""
Reset the instance count.
"""
cls._instance_count = 0
@asyncio.coroutine
def create(self):
port = yield from self._hypervisor.send("nio create_udp_auto {name} {laddr} {lport_start} {lport_end}".format(name=self._name,
laddr=self._laddr,
lport_start=self._lport_start,
lport_end=self._lport_end))
self._lport = int(port[0])
log.info("NIO UDP AUTO {name} created with laddr={laddr}, lport_start={start}, lport_end={end}".format(name=self._name,
laddr=self._laddr,
start=self._lport_start,
end=self._lport_end))
@property
def laddr(self):
"""
Returns the local address
:returns: local address
"""
return self._laddr
@property
def lport(self):
"""
Returns the local port
:returns: local port number
"""
return self._lport
@property
def raddr(self):
"""
Returns the remote address
:returns: remote address
"""
return self._raddr
@property
def rport(self):
"""
Returns the remote port
:returns: remote port number
"""
return self._rport
@asyncio.coroutine
def connect(self, raddr, rport):
"""
Connects this NIO to a remote socket
:param raddr: remote address
:param rport: remote port number
"""
yield from self._hypervisor.send("nio connect_udp_auto {name} {raddr} {rport}".format(name=self._name,
raddr=raddr,
rport=rport))
self._raddr = raddr
self._rport = rport
log.info("NIO UDP AUTO {name} connected to {raddr}:{rport}".format(name=self._name,
raddr=raddr,
rport=rport))
def __json__(self):
return {"type": "nio_udp_auto",
"lport": self._lport,
"rport": self._rport,
"raddr": self._raddr}
@@ -24,6 +24,7 @@ import asyncio
import re
from .device import Device
from ..nios.nio_udp import NIOUDP
from ..dynamips_error import DynamipsError
import logging
@@ -106,6 +107,10 @@ class ATMSwitch(Device):
Deletes this ATM switch.
"""
for nio in self._nios.values():
if nio and isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
try:
yield from self._hypervisor.send('atmsw delete "{}"'.format(self._name))
log.info('ATM switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
@@ -155,6 +160,8 @@ class ATMSwitch(Device):
raise DynamipsError("Port {} is not allocated".format(port_number))
nio = self._nios[port_number]
if isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
log.info('ATM switch "{name}" [{id}]: NIO {nio} removed from port {port}'.format(name=self._name,
id=self._id,
nio=nio,
@@ -22,6 +22,7 @@ Hub object that uses the Bridge interface to create a hub with ports.
import asyncio
from .bridge import Bridge
from ..nios.nio_udp import NIOUDP
from ..dynamips_error import DynamipsError
import logging
@@ -73,6 +74,10 @@ class EthernetHub(Bridge):
Deletes this hub.
"""
for nio in self._nios.values():
if nio and isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
try:
yield from Bridge.delete(self)
log.info('Ethernet hub "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
@@ -115,6 +120,8 @@ class EthernetHub(Bridge):
raise DynamipsError("Port {} is not allocated".format(port_number))
nio = self._mappings[port_number]
if isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
yield from Bridge.remove_nio(self, nio)
log.info('Ethernet switch "{name}" [{id}]: NIO {nio} removed from port {port}'.format(name=self._name,
@@ -23,6 +23,7 @@ http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L558
import asyncio
from .device import Device
from ..nios.nio_udp import NIOUDP
from ..dynamips_error import DynamipsError
@@ -114,6 +115,10 @@ class EthernetSwitch(Device):
Deletes this Ethernet switch.
"""
for nio in self._nios.values():
if nio and isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
try:
yield from self._hypervisor.send('ethsw delete "{}"'.format(self._name))
log.info('Ethernet switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
@@ -157,6 +162,8 @@ class EthernetSwitch(Device):
raise DynamipsError("Port {} is not allocated".format(port_number))
nio = self._nios[port_number]
if isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
yield from self._hypervisor.send('ethsw remove_nio "{name}" {nio}'.format(name=self._name, nio=nio))
log.info('Ethernet switch "{name}" [{id}]: NIO {nio} removed from port {port}'.format(name=self._name,
@@ -23,6 +23,7 @@ http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L642
import asyncio
from .device import Device
from ..nios.nio_udp import NIOUDP
from ..dynamips_error import DynamipsError
import logging
@@ -105,6 +106,10 @@ class FrameRelaySwitch(Device):
Deletes this Frame Relay switch.
"""
for nio in self._nios.values():
if nio and isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
try:
yield from self._hypervisor.send('frsw delete "{}"'.format(self._name))
log.info('Frame Relay switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
@@ -156,6 +161,9 @@ class FrameRelaySwitch(Device):
raise DynamipsError("Port {} is not allocated".format(port_number))
nio = self._nios[port_number]
if isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
log.info('Frame Relay switch "{name}" [{id}]: NIO {nio} removed from port {port}'.format(name=self._name,
id=self._id,
nio=nio,
+17 -8
View File
@@ -32,6 +32,7 @@ log = logging.getLogger(__name__)
from ...base_vm import BaseVM
from ..dynamips_error import DynamipsError
from ..nios.nio_udp import NIOUDP
from gns3server.utils.asyncio import wait_run_in_executor
@@ -312,6 +313,20 @@ class Router(BaseVM):
if self._dynamips_id in self._dynamips_ids[self._project.id]:
self._dynamips_ids[self._project.id].remove(self._dynamips_id)
if self._console:
self._manager.port_manager.release_tcp_port(self._console)
self._console = None
if self._aux:
self._manager.port_manager.release_tcp_port(self._aux)
self._aux = None
for adapter in self._slots:
if adapter is not None:
for nio in adapter.ports.values():
if nio and isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
if self in self._hypervisor.devices:
self._hypervisor.devices.remove(self)
if self._hypervisor and not self._hypervisor.devices:
@@ -323,14 +338,6 @@ class Router(BaseVM):
pass
yield from self.hypervisor.stop()
if self._console:
self._manager.port_manager.release_tcp_port(self._console)
self._console = None
if self._aux:
self._manager.port_manager.release_tcp_port(self._aux)
self._aux = None
self._closed = True
@property
@@ -1226,6 +1233,8 @@ class Router(BaseVM):
port_number=port_number))
nio = adapter.get_nio(port_number)
if isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport)
adapter.remove_nio(port_number)
log.info('Router "{name}" [{id}]: NIO {nio_name} removed from port {slot_number}/{port_number}'.format(name=self._name,