2015-02-14 03:01:18 +00:00
|
|
|
# -*- 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Hub object that uses the Bridge interface to create a hub with ports.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from .bridge import Bridge
|
2015-02-24 02:00:34 +00:00
|
|
|
from ..nios.nio_udp import NIOUDP
|
2015-02-14 03:01:18 +00:00
|
|
|
from ..dynamips_error import DynamipsError
|
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class EthernetHub(Bridge):
|
2015-02-23 16:21:39 +00:00
|
|
|
|
2015-02-14 03:01:18 +00:00
|
|
|
"""
|
|
|
|
Dynamips Ethernet hub (based on Bridge)
|
|
|
|
|
|
|
|
:param name: name for this hub
|
2016-05-20 04:45:04 +00:00
|
|
|
:param node_id: Node instance identifier
|
2015-02-14 03:01:18 +00:00
|
|
|
:param project: Project instance
|
|
|
|
:param manager: Parent VM Manager
|
2016-05-20 04:45:04 +00:00
|
|
|
:param ports: initial hub ports
|
2015-02-14 03:01:18 +00:00
|
|
|
:param hypervisor: Dynamips hypervisor instance
|
|
|
|
"""
|
|
|
|
|
2016-05-20 04:45:04 +00:00
|
|
|
def __init__(self, name, node_id, project, manager, ports=None, hypervisor=None):
|
2015-02-14 03:01:18 +00:00
|
|
|
|
2016-05-20 04:45:04 +00:00
|
|
|
super().__init__(name, node_id, project, manager, hypervisor)
|
2015-02-15 19:18:12 +00:00
|
|
|
self._mappings = {}
|
2016-05-20 04:45:04 +00:00
|
|
|
if ports is None:
|
|
|
|
# create 8 ports by default
|
|
|
|
self._ports = []
|
|
|
|
for port_number in range(1, 9):
|
|
|
|
self._ports.append({"port_number": port_number,
|
|
|
|
"name": "Ethernet{}".format(port_number)})
|
|
|
|
else:
|
|
|
|
self._ports = ports
|
2015-02-15 19:18:12 +00:00
|
|
|
|
|
|
|
def __json__(self):
|
|
|
|
|
|
|
|
return {"name": self.name,
|
2016-05-18 03:22:18 +00:00
|
|
|
"node_id": self.id,
|
2016-05-20 04:45:04 +00:00
|
|
|
"project_id": self.project.id,
|
|
|
|
"ports": self._ports,
|
|
|
|
"status": "started"}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ports(self):
|
|
|
|
"""
|
|
|
|
Ports on this hub
|
|
|
|
|
|
|
|
:returns: ports info
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._ports
|
|
|
|
|
|
|
|
@ports.setter
|
|
|
|
def ports(self, ports):
|
|
|
|
"""
|
|
|
|
Set the ports on this hub
|
|
|
|
|
|
|
|
:param ports: ports info
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._ports = ports
|
2015-02-14 03:01:18 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def create(self):
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
yield from Bridge.create(self)
|
2015-02-14 03:01:18 +00:00
|
|
|
log.info('Ethernet hub "{name}" [{id}] has been created'.format(name=self._name, id=self._id))
|
|
|
|
|
|
|
|
@property
|
2015-02-15 19:18:12 +00:00
|
|
|
def mappings(self):
|
2015-02-14 03:01:18 +00:00
|
|
|
"""
|
2015-02-15 19:18:12 +00:00
|
|
|
Returns port mappings
|
2015-02-14 03:01:18 +00:00
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
:returns: mappings list
|
2015-02-14 03:01:18 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
return self._mappings
|
2015-02-14 03:01:18 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def delete(self):
|
|
|
|
"""
|
|
|
|
Deletes this hub.
|
|
|
|
"""
|
|
|
|
|
2015-02-25 18:52:52 +00:00
|
|
|
for nio in self._nios:
|
2015-02-24 02:00:34 +00:00
|
|
|
if nio and isinstance(nio, NIOUDP):
|
2015-03-21 23:19:12 +00:00
|
|
|
self.manager.port_manager.release_udp_port(nio.lport, self._project)
|
2015-02-24 02:00:34 +00:00
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
try:
|
|
|
|
yield from Bridge.delete(self)
|
|
|
|
log.info('Ethernet hub "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
|
|
|
|
except DynamipsError:
|
|
|
|
log.debug("Could not properly delete Ethernet hub {}".format(self._name))
|
|
|
|
if self._hypervisor and not self._hypervisor.devices:
|
|
|
|
yield from self.hypervisor.stop()
|
2015-02-14 03:01:18 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def add_nio(self, nio, port_number):
|
|
|
|
"""
|
|
|
|
Adds a NIO as new port on this hub.
|
|
|
|
|
|
|
|
:param nio: NIO instance to add
|
|
|
|
:param port_number: port to allocate for the NIO
|
|
|
|
"""
|
|
|
|
|
2016-05-20 04:45:04 +00:00
|
|
|
if port_number not in [port["port_number"] for port in self._ports]:
|
|
|
|
raise DynamipsError("Port {} doesn't exist".format(port_number))
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
if port_number in self._mappings:
|
2015-02-14 03:01:18 +00:00
|
|
|
raise DynamipsError("Port {} isn't free".format(port_number))
|
|
|
|
|
|
|
|
yield from Bridge.add_nio(self, nio)
|
|
|
|
|
|
|
|
log.info('Ethernet hub "{name}" [{id}]: NIO {nio} bound to port {port}'.format(name=self._name,
|
|
|
|
id=self._id,
|
|
|
|
nio=nio,
|
|
|
|
port=port_number))
|
2015-02-15 19:18:12 +00:00
|
|
|
self._mappings[port_number] = nio
|
2015-02-14 03:01:18 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def remove_nio(self, port_number):
|
|
|
|
"""
|
|
|
|
Removes the specified NIO as member of this hub.
|
|
|
|
|
|
|
|
:param port_number: allocated port number
|
|
|
|
|
|
|
|
:returns: the NIO that was bound to the allocated port
|
|
|
|
"""
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
if port_number not in self._mappings:
|
2015-02-14 03:01:18 +00:00
|
|
|
raise DynamipsError("Port {} is not allocated".format(port_number))
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
nio = self._mappings[port_number]
|
2015-02-24 02:00:34 +00:00
|
|
|
if isinstance(nio, NIOUDP):
|
2015-03-21 23:19:12 +00:00
|
|
|
self.manager.port_manager.release_udp_port(nio.lport, self._project)
|
2015-02-14 03:01:18 +00:00
|
|
|
yield from Bridge.remove_nio(self, nio)
|
|
|
|
|
|
|
|
log.info('Ethernet switch "{name}" [{id}]: NIO {nio} removed from port {port}'.format(name=self._name,
|
|
|
|
id=self._id,
|
|
|
|
nio=nio,
|
|
|
|
port=port_number))
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
del self._mappings[port_number]
|
2015-02-14 03:01:18 +00:00
|
|
|
return nio
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def start_capture(self, port_number, output_file, data_link_type="DLT_EN10MB"):
|
|
|
|
"""
|
|
|
|
Starts a packet capture.
|
|
|
|
|
|
|
|
:param port_number: allocated port number
|
|
|
|
:param output_file: PCAP destination file for the capture
|
|
|
|
:param data_link_type: PCAP data link type (DLT_*), default is DLT_EN10MB
|
|
|
|
"""
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
if port_number not in self._mappings:
|
2015-02-14 03:01:18 +00:00
|
|
|
raise DynamipsError("Port {} is not allocated".format(port_number))
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
nio = self._mappings[port_number]
|
2015-02-14 03:01:18 +00:00
|
|
|
|
|
|
|
data_link_type = data_link_type.lower()
|
|
|
|
if data_link_type.startswith("dlt_"):
|
|
|
|
data_link_type = data_link_type[4:]
|
|
|
|
|
|
|
|
if nio.input_filter[0] is not None and nio.output_filter[0] is not None:
|
|
|
|
raise DynamipsError("Port {} has already a filter applied".format(port_number))
|
|
|
|
|
|
|
|
yield from nio.bind_filter("both", "capture")
|
2015-02-25 06:12:09 +00:00
|
|
|
yield from nio.setup_filter("both", '{} "{}"'.format(data_link_type, output_file))
|
2015-02-14 03:01:18 +00:00
|
|
|
|
2015-02-16 00:45:53 +00:00
|
|
|
log.info('Ethernet hub "{name}" [{id}]: starting packet capture on port {port}'.format(name=self._name,
|
|
|
|
id=self._id,
|
|
|
|
port=port_number))
|
2015-02-14 03:01:18 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def stop_capture(self, port_number):
|
|
|
|
"""
|
|
|
|
Stops a packet capture.
|
|
|
|
|
|
|
|
:param port_number: allocated port number
|
|
|
|
"""
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
if port_number not in self._mappings:
|
2015-02-14 03:01:18 +00:00
|
|
|
raise DynamipsError("Port {} is not allocated".format(port_number))
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
nio = self._mappings[port_number]
|
2015-02-14 03:01:18 +00:00
|
|
|
yield from nio.unbind_filter("both")
|
2015-02-16 00:45:53 +00:00
|
|
|
log.info('Ethernet hub "{name}" [{id}]: stopping packet capture on port {port}'.format(name=self._name,
|
|
|
|
id=self._id,
|
|
|
|
port=port_number))
|