1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-04 22:28:12 +00:00
gns3-server/gns3server/compute/dynamips/nodes/ethernet_hub.py

242 lines
7.0 KiB
Python
Raw Normal View History

2015-02-14 03:01:18 +00:00
#
# 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.
"""
from .bridge import Bridge
from ..nios.nio_udp import NIOUDP
2015-02-14 03:01:18 +00:00
from ..dynamips_error import DynamipsError
2016-09-29 14:01:18 +00:00
from ...error import NodeError
2015-02-14 03:01:18 +00:00
import logging
2021-04-13 09:16:50 +00:00
2015-02-14 03:01:18 +00:00
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
:param node_id: Node instance identifier
2015-02-14 03:01:18 +00:00
:param project: Project instance
:param manager: Parent VM Manager
:param ports: initial hub ports
2015-02-14 03:01:18 +00:00
:param hypervisor: Dynamips hypervisor instance
"""
def __init__(self, name, node_id, project, manager, ports=None, hypervisor=None):
2015-02-14 03:01:18 +00:00
super().__init__(name, node_id, project, manager, hypervisor)
self._mappings = {}
if ports is None:
# create 8 ports by default
self._ports = []
for port_number in range(0, 8):
2021-04-13 09:16:50 +00:00
self._ports.append({"port_number": port_number, "name": f"Ethernet{port_number}"})
else:
self._ports = ports
2021-04-17 14:04:28 +00:00
def asdict(self):
2021-04-13 09:16:50 +00:00
return {
"name": self.name,
"usage": self.usage,
"node_id": self.id,
"project_id": self.project.id,
"ports_mapping": self._ports,
"status": "started",
}
@property
2016-09-29 14:01:18 +00:00
def ports_mapping(self):
"""
Ports on this hub
:returns: ports info
"""
return self._ports
2016-09-29 14:01:18 +00:00
@ports_mapping.setter
def ports_mapping(self, ports):
"""
Set the ports on this hub
:param ports: ports info
"""
2016-09-29 14:01:18 +00:00
if ports != self._ports:
if len(self._mappings) > 0:
raise NodeError("Can't modify a hub already connected.")
2016-09-29 14:01:18 +00:00
port_number = 0
for port in ports:
2021-04-13 09:07:58 +00:00
port["name"] = f"Ethernet{port_number}"
2016-09-29 14:01:18 +00:00
port["port_number"] = port_number
port_number += 1
self._ports = ports
2015-02-14 03:01:18 +00:00
async def create(self):
2015-02-14 03:01:18 +00:00
await Bridge.create(self)
2021-04-13 09:07:58 +00:00
log.info(f'Ethernet hub "{self._name}" [{self._id}] has been created')
2015-02-14 03:01:18 +00:00
@property
def mappings(self):
2015-02-14 03:01:18 +00:00
"""
Returns port mappings
2015-02-14 03:01:18 +00:00
:returns: mappings list
2015-02-14 03:01:18 +00:00
"""
return self._mappings
2015-02-14 03:01:18 +00:00
async def delete(self):
2021-04-13 09:16:50 +00:00
return await self.close()
async def close(self):
2015-02-14 03:01:18 +00:00
"""
Deletes this hub.
"""
for nio in self._nios:
if nio:
await nio.close()
try:
await Bridge.delete(self)
2021-04-13 09:07:58 +00:00
log.info(f'Ethernet hub "{self._name}" [{self._id}] has been deleted')
except DynamipsError:
2021-04-13 09:07:58 +00:00
log.debug(f"Could not properly delete Ethernet hub {self._name}")
if self._hypervisor and not self._hypervisor.devices:
await self.hypervisor.stop()
self._hypervisor = None
return True
2015-02-14 03:01:18 +00:00
async def add_nio(self, nio, port_number):
2015-02-14 03:01:18 +00:00
"""
Adds a NIO as new port on this hub.
:param nio: NIO instance to add
:param port_number: port to allocate for the NIO
"""
if port_number not in [port["port_number"] for port in self._ports]:
2021-04-13 09:07:58 +00:00
raise DynamipsError(f"Port {port_number} doesn't exist")
if port_number in self._mappings:
2021-04-13 09:07:58 +00:00
raise DynamipsError(f"Port {port_number} isn't free")
2015-02-14 03:01:18 +00:00
await Bridge.add_nio(self, nio)
2015-02-14 03:01:18 +00:00
2021-04-13 09:16:50 +00:00
log.info(
'Ethernet hub "{name}" [{id}]: NIO {nio} bound to port {port}'.format(
name=self._name, id=self._id, nio=nio, port=port_number
)
)
self._mappings[port_number] = nio
2015-02-14 03:01:18 +00:00
async def remove_nio(self, port_number):
2015-02-14 03:01:18 +00:00
"""
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
"""
if port_number not in self._mappings:
2021-04-13 09:07:58 +00:00
raise DynamipsError(f"Port {port_number} is not allocated")
2015-02-14 03:01:18 +00:00
await self.stop_capture(port_number)
nio = self._mappings[port_number]
if isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport, self._project)
await Bridge.remove_nio(self, nio)
2015-02-14 03:01:18 +00:00
2021-04-13 09:16:50 +00:00
log.info(
'Ethernet hub "{name}" [{id}]: NIO {nio} removed from port {port}'.format(
name=self._name, id=self._id, nio=nio, port=port_number
)
)
2015-02-14 03:01:18 +00:00
del self._mappings[port_number]
2015-02-14 03:01:18 +00:00
return nio
def get_nio(self, port_number):
2015-02-14 03:01:18 +00:00
"""
Gets a port NIO binding.
2015-02-14 03:01:18 +00:00
:param port_number: port number
:returns: NIO instance
2015-02-14 03:01:18 +00:00
"""
if port_number not in self._mappings:
2021-04-13 09:07:58 +00:00
raise DynamipsError(f"Port {port_number} is not allocated")
2015-02-14 03:01:18 +00:00
nio = self._mappings[port_number]
2015-02-14 03:01:18 +00:00
if not nio:
2021-04-13 09:07:58 +00:00
raise DynamipsError(f"Port {port_number} is not connected")
return nio
async 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
"""
nio = self.get_nio(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:
2021-04-13 09:07:58 +00:00
raise DynamipsError(f"Port {port_number} has already a filter applied")
2015-02-14 03:01:18 +00:00
await nio.start_packet_capture(output_file, data_link_type)
2021-04-13 09:16:50 +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
async def stop_capture(self, port_number):
2015-02-14 03:01:18 +00:00
"""
Stops a packet capture.
:param port_number: allocated port number
"""
nio = self.get_nio(port_number)
if not nio.capturing:
return
await nio.stop_packet_capture()
2021-04-13 09:16:50 +00:00
log.info(
'Ethernet hub "{name}" [{id}]: stopping packet capture on port {port}'.format(
name=self._name, id=self._id, port=port_number
)
)