2016-05-23 01:24:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2016-06-13 21:07:20 +00:00
|
|
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
2016-05-23 01:24:14 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
import sys
|
2016-05-23 01:24:14 +00:00
|
|
|
import asyncio
|
2016-06-10 18:36:41 +00:00
|
|
|
import subprocess
|
2016-05-23 01:24:14 +00:00
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
from ...error import NodeError
|
2016-05-23 01:24:14 +00:00
|
|
|
from ...base_node import BaseNode
|
2016-05-29 03:46:48 +00:00
|
|
|
from ...nios.nio_udp import NIOUDP
|
2016-09-23 08:52:08 +00:00
|
|
|
from ....ubridge.ubridge_error import UbridgeError
|
2016-05-29 03:46:48 +00:00
|
|
|
|
2016-09-22 15:46:32 +00:00
|
|
|
import gns3server.utils.interfaces
|
2016-06-10 18:36:41 +00:00
|
|
|
import gns3server.utils.asyncio
|
2016-05-23 01:24:14 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Cloud(BaseNode):
|
|
|
|
|
|
|
|
"""
|
|
|
|
Cloud.
|
|
|
|
|
|
|
|
:param name: name for this cloud
|
|
|
|
:param node_id: Node identifier
|
|
|
|
:param project: Project instance
|
|
|
|
:param manager: Parent VM Manager
|
|
|
|
"""
|
|
|
|
|
2016-09-22 15:46:32 +00:00
|
|
|
def __init__(self, name, node_id, project, manager, ports=[]):
|
2016-05-23 01:24:14 +00:00
|
|
|
|
|
|
|
super().__init__(name, node_id, project, manager)
|
2016-05-29 03:46:48 +00:00
|
|
|
self._nios = {}
|
2016-09-22 15:46:32 +00:00
|
|
|
# If the cloud is not configured we fill it with host interfaces
|
|
|
|
if not ports or len(ports) == 0:
|
|
|
|
self._ports_mapping = []
|
2016-09-22 16:29:46 +00:00
|
|
|
for interface in self._interfaces():
|
2016-09-22 15:46:32 +00:00
|
|
|
if not interface["special"]:
|
|
|
|
self._ports_mapping.append({
|
|
|
|
"interface": interface["name"],
|
|
|
|
"type": interface["type"],
|
|
|
|
"port_number": len(self._ports_mapping),
|
|
|
|
"name": interface["name"]
|
|
|
|
})
|
|
|
|
else:
|
2016-09-27 09:54:07 +00:00
|
|
|
port_number = 0
|
|
|
|
for port in ports:
|
|
|
|
port["port_number"] = port_number
|
|
|
|
port_number += 1
|
|
|
|
self._ports_mapping = ports
|
2016-05-23 01:24:14 +00:00
|
|
|
|
2016-09-22 16:29:46 +00:00
|
|
|
def _interfaces(self):
|
|
|
|
return gns3server.utils.interfaces.interfaces()
|
|
|
|
|
2016-05-23 01:24:14 +00:00
|
|
|
def __json__(self):
|
|
|
|
|
2016-05-29 03:46:48 +00:00
|
|
|
host_interfaces = []
|
2016-09-22 15:46:32 +00:00
|
|
|
network_interfaces = gns3server.utils.interfaces.interfaces()
|
2016-05-29 03:46:48 +00:00
|
|
|
for interface in network_interfaces:
|
|
|
|
host_interfaces.append({"name": interface["name"],
|
2016-09-14 15:15:06 +00:00
|
|
|
"type": interface["type"],
|
|
|
|
"special": interface["special"]})
|
2016-05-29 03:46:48 +00:00
|
|
|
|
2016-05-23 01:24:14 +00:00
|
|
|
return {"name": self.name,
|
|
|
|
"node_id": self.id,
|
2016-05-29 03:46:48 +00:00
|
|
|
"project_id": self.project.id,
|
2016-09-13 07:47:22 +00:00
|
|
|
"ports_mapping": self._ports_mapping,
|
2016-05-29 22:35:07 +00:00
|
|
|
"interfaces": host_interfaces,
|
|
|
|
"status": "started"}
|
2016-05-29 03:46:48 +00:00
|
|
|
|
|
|
|
@property
|
2016-09-13 07:47:22 +00:00
|
|
|
def ports_mapping(self):
|
2016-05-29 03:46:48 +00:00
|
|
|
"""
|
|
|
|
Ports on this cloud.
|
|
|
|
|
|
|
|
:returns: ports info
|
|
|
|
"""
|
|
|
|
|
2016-09-13 07:47:22 +00:00
|
|
|
return self._ports_mapping
|
2016-05-29 03:46:48 +00:00
|
|
|
|
2016-09-13 07:47:22 +00:00
|
|
|
@ports_mapping.setter
|
|
|
|
def ports_mapping(self, ports):
|
2016-05-29 03:46:48 +00:00
|
|
|
"""
|
|
|
|
Set the ports on this cloud.
|
|
|
|
|
|
|
|
:param ports: ports info
|
|
|
|
"""
|
|
|
|
|
2016-09-29 14:01:18 +00:00
|
|
|
if ports != self._ports_mapping:
|
|
|
|
if len(self._nios) > 0:
|
|
|
|
raise NodeError("Can't modify a cloud already connected.")
|
2016-09-23 08:52:08 +00:00
|
|
|
|
2016-09-29 14:01:18 +00:00
|
|
|
port_number = 0
|
|
|
|
for port in ports:
|
|
|
|
port["port_number"] = port_number
|
|
|
|
port_number += 1
|
2016-09-27 09:21:40 +00:00
|
|
|
|
2016-09-29 14:01:18 +00:00
|
|
|
self._ports_mapping = ports
|
2016-05-23 01:24:14 +00:00
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
@asyncio.coroutine
|
2016-05-23 01:24:14 +00:00
|
|
|
def create(self):
|
|
|
|
"""
|
|
|
|
Creates this cloud.
|
|
|
|
"""
|
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
yield from self._start_ubridge()
|
2016-05-23 01:24:14 +00:00
|
|
|
log.info('Cloud "{name}" [{id}] has been created'.format(name=self._name, id=self._id))
|
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def close(self):
|
2016-05-23 01:24:14 +00:00
|
|
|
"""
|
2016-05-29 22:35:07 +00:00
|
|
|
Closes this cloud.
|
2016-05-23 01:24:14 +00:00
|
|
|
"""
|
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
if not (yield from super().close()):
|
|
|
|
return False
|
|
|
|
|
2016-05-29 03:46:48 +00:00
|
|
|
for nio in self._nios.values():
|
|
|
|
if nio and isinstance(nio, NIOUDP):
|
|
|
|
self.manager.port_manager.release_udp_port(nio.lport, self._project)
|
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
yield from self._stop_ubridge()
|
|
|
|
log.info('Cloud "{name}" [{id}] has been closed'.format(name=self._name, id=self._id))
|
|
|
|
|
2016-06-10 18:36:41 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def _is_wifi_adapter_osx(self, adapter_name):
|
|
|
|
|
|
|
|
try:
|
|
|
|
output = yield from gns3server.utils.asyncio.subprocess_check_output("networksetup", "-listallhardwareports")
|
|
|
|
except (FileNotFoundError, subprocess.SubprocessError) as e:
|
|
|
|
log.warn("Could not execute networksetup: {}".format(e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
is_wifi = False
|
|
|
|
for line in output.splitlines():
|
|
|
|
if is_wifi:
|
|
|
|
if adapter_name == line.replace("Device: ", ""):
|
|
|
|
return True
|
|
|
|
is_wifi = False
|
|
|
|
else:
|
|
|
|
if 'Wi-Fi' in line:
|
|
|
|
is_wifi = True
|
|
|
|
return False
|
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def _add_ubridge_connection(self, nio, port_number):
|
|
|
|
"""
|
|
|
|
Creates a connection in uBridge.
|
|
|
|
|
|
|
|
:param nio: NIO instance
|
|
|
|
:param port_number: port number
|
|
|
|
"""
|
|
|
|
|
|
|
|
port_info = None
|
2016-09-18 20:16:14 +00:00
|
|
|
for port in self._ports_mapping:
|
2016-05-29 22:35:07 +00:00
|
|
|
if port["port_number"] == port_number:
|
|
|
|
port_info = port
|
|
|
|
break
|
|
|
|
|
|
|
|
if not port_info:
|
|
|
|
raise NodeError("Port {port_number} doesn't exist on cloud '{name}'".format(name=self.name,
|
|
|
|
port_number=port_number))
|
|
|
|
|
|
|
|
bridge_name = "{}-{}".format(self._id, port_number)
|
|
|
|
yield from self._ubridge_send("bridge create {name}".format(name=bridge_name))
|
|
|
|
if not isinstance(nio, NIOUDP):
|
|
|
|
raise NodeError("Source NIO is not UDP")
|
|
|
|
yield from self._ubridge_send('bridge add_nio_udp {name} {lport} {rhost} {rport}'.format(name=bridge_name,
|
|
|
|
lport=nio.lport,
|
|
|
|
rhost=nio.rhost,
|
|
|
|
rport=nio.rport))
|
|
|
|
|
|
|
|
if port_info["type"] in ("ethernet", "tap"):
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
2016-09-22 16:29:46 +00:00
|
|
|
windows_interfaces = self._interfaces()
|
2016-05-29 22:35:07 +00:00
|
|
|
npf = None
|
|
|
|
for interface in windows_interfaces:
|
|
|
|
if port_info["interface"] == interface["name"]:
|
|
|
|
npf = interface["id"]
|
|
|
|
if npf:
|
|
|
|
yield from self._ubridge_send('bridge add_nio_ethernet {name} "{interface}"'.format(name=bridge_name,
|
|
|
|
interface=npf))
|
|
|
|
else:
|
|
|
|
raise NodeError("Could not find NPF id for interface {}".format(port_info["interface"]))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if port_info["type"] == "ethernet":
|
2016-09-22 16:29:46 +00:00
|
|
|
network_interfaces = [interface["name"] for interface in self._interfaces()]
|
2016-06-23 17:36:32 +00:00
|
|
|
if not port_info["interface"] in network_interfaces:
|
|
|
|
raise NodeError("Interface '{}' could not be found on this system".format(port_info["interface"]))
|
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
if sys.platform.startswith("linux"):
|
|
|
|
# use raw sockets on Linux
|
|
|
|
yield from self._ubridge_send('bridge add_nio_linux_raw {name} "{interface}"'.format(name=bridge_name,
|
|
|
|
interface=port_info["interface"]))
|
|
|
|
else:
|
2016-06-10 18:36:41 +00:00
|
|
|
if sys.platform.startswith("darwin"):
|
2016-06-10 01:00:33 +00:00
|
|
|
# Wireless adapters are not well supported by the libpcap on OSX
|
2016-06-10 18:36:41 +00:00
|
|
|
if (yield from self._is_wifi_adapter_osx(port_info["interface"])):
|
|
|
|
raise NodeError("Connecting to a Wireless adapter is not supported on Mac OS")
|
2016-06-10 01:00:33 +00:00
|
|
|
if sys.platform.startswith("darwin") and port_info["interface"].startswith("vmnet"):
|
|
|
|
# Use a special NIO to connect to VMware vmnet interfaces on OSX (libpcap doesn't support them)
|
|
|
|
yield from self._ubridge_send('bridge add_nio_fusion_vmnet {name} "{interface}"'.format(name=bridge_name,
|
|
|
|
interface=port_info["interface"]))
|
|
|
|
else:
|
2016-09-27 09:47:18 +00:00
|
|
|
if not gns3server.utils.interfaces.has_netmask(port_info["interface"]):
|
|
|
|
raise NodeError("Interface {} don't have a netmask".format(port_info["interface"]))
|
2016-06-10 01:00:33 +00:00
|
|
|
yield from self._ubridge_send('bridge add_nio_ethernet {name} "{interface}"'.format(name=bridge_name,
|
|
|
|
interface=port_info["interface"]))
|
2016-05-29 22:35:07 +00:00
|
|
|
|
|
|
|
elif port_info["type"] == "tap":
|
2016-09-23 15:08:05 +00:00
|
|
|
yield from self._ubridge_send('bridge add_nio_tap {name} "{interface}"'.format(name=bridge_name, interface=port_info["interface"]))
|
2016-05-29 22:35:07 +00:00
|
|
|
|
|
|
|
elif port_info["type"] == "udp":
|
|
|
|
yield from self._ubridge_send('bridge add_nio_udp {name} {lport} {rhost} {rport}'.format(name=bridge_name,
|
|
|
|
lport=port_info["lport"],
|
|
|
|
rhost=port_info["rhost"],
|
|
|
|
rport=port_info["rport"]))
|
|
|
|
|
|
|
|
if nio.capturing:
|
|
|
|
yield from self._ubridge_send('bridge start_capture {name} "{pcap_file}"'.format(name=bridge_name,
|
|
|
|
pcap_file=nio.pcap_output_file))
|
|
|
|
|
|
|
|
yield from self._ubridge_send('bridge start {name}'.format(name=bridge_name))
|
2016-05-23 01:24:14 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def add_nio(self, nio, port_number):
|
|
|
|
"""
|
|
|
|
Adds a NIO as new port on this cloud.
|
|
|
|
|
|
|
|
:param nio: NIO instance to add
|
|
|
|
:param port_number: port to allocate for the NIO
|
|
|
|
"""
|
|
|
|
|
2016-05-29 03:46:48 +00:00
|
|
|
if port_number in self._nios:
|
|
|
|
raise NodeError("Port {} isn't free".format(port_number))
|
|
|
|
|
|
|
|
log.info('Cloud "{name}" [{id}]: NIO {nio} bound to port {port}'.format(name=self._name,
|
|
|
|
id=self._id,
|
|
|
|
nio=nio,
|
|
|
|
port=port_number))
|
|
|
|
self._nios[port_number] = nio
|
2016-09-23 08:52:08 +00:00
|
|
|
try:
|
|
|
|
yield from self._add_ubridge_connection(nio, port_number)
|
2016-10-03 10:31:01 +00:00
|
|
|
except NodeError as e:
|
|
|
|
del self._nios[port_number]
|
|
|
|
raise e
|
2016-09-23 08:52:08 +00:00
|
|
|
# Cleanup stuff
|
|
|
|
except UbridgeError as e:
|
|
|
|
try:
|
|
|
|
self._delete_ubridge_connection(port_number)
|
|
|
|
except UbridgeError:
|
|
|
|
pass
|
|
|
|
del self._nios[port_number]
|
|
|
|
raise e
|
2016-05-29 22:35:07 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def _delete_ubridge_connection(self, port_number):
|
|
|
|
"""
|
|
|
|
Deletes a connection in uBridge.
|
|
|
|
|
|
|
|
:param port_number: adapter number
|
|
|
|
"""
|
2016-05-29 03:46:48 +00:00
|
|
|
|
2016-05-29 22:35:07 +00:00
|
|
|
bridge_name = "{}-{}".format(self._id, port_number)
|
|
|
|
yield from self._ubridge_send("bridge delete {name}".format(name=bridge_name))
|
2016-05-23 01:24:14 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def remove_nio(self, port_number):
|
|
|
|
"""
|
|
|
|
Removes the specified NIO as member of cloud.
|
|
|
|
|
|
|
|
:param port_number: allocated port number
|
|
|
|
|
|
|
|
:returns: the NIO that was bound to the allocated port
|
|
|
|
"""
|
|
|
|
|
2016-05-29 03:46:48 +00:00
|
|
|
if port_number not in self._nios:
|
|
|
|
raise NodeError("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, self._project)
|
|
|
|
|
|
|
|
log.info('Cloud "{name}" [{id}]: NIO {nio} removed from port {port}'.format(name=self._name,
|
|
|
|
id=self._id,
|
|
|
|
nio=nio,
|
|
|
|
port=port_number))
|
|
|
|
|
|
|
|
del self._nios[port_number]
|
2016-05-29 22:35:07 +00:00
|
|
|
yield from self._delete_ubridge_connection(port_number)
|
2016-05-29 03:46:48 +00:00
|
|
|
return nio
|
2016-05-23 01:24:14 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
"""
|
|
|
|
|
2016-09-18 20:16:14 +00:00
|
|
|
if not [port["port_number"] for port in self._ports_mapping if port_number == port["port_number"]]:
|
2016-05-29 22:35:07 +00:00
|
|
|
raise NodeError("Port {port_number} doesn't exist on cloud '{name}'".format(name=self.name,
|
|
|
|
port_number=port_number))
|
|
|
|
|
|
|
|
if port_number not in self._nios:
|
|
|
|
raise NodeError("Port {} is not connected".format(port_number))
|
|
|
|
|
|
|
|
nio = self._nios[port_number]
|
|
|
|
|
|
|
|
if nio.capturing:
|
|
|
|
raise NodeError("Packet capture is already activated on port {port_number}".format(port_number=port_number))
|
|
|
|
nio.startPacketCapture(output_file)
|
|
|
|
bridge_name = "{}-{}".format(self._id, port_number)
|
|
|
|
yield from self._ubridge_send('bridge start_capture {name} "{output_file}"'.format(name=bridge_name,
|
|
|
|
output_file=output_file))
|
|
|
|
log.info("Cloud '{name}' [{id}]: starting packet capture on port {port_number}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
port_number=port_number))
|
|
|
|
|
2016-05-23 01:24:14 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def stop_capture(self, port_number):
|
|
|
|
"""
|
|
|
|
Stops a packet capture.
|
|
|
|
|
|
|
|
:param port_number: allocated port number
|
|
|
|
"""
|
|
|
|
|
2016-09-18 20:16:14 +00:00
|
|
|
if not [port["port_number"] for port in self._ports_mapping if port_number == port["port_number"]]:
|
2016-05-29 22:35:07 +00:00
|
|
|
raise NodeError("Port {port_number} doesn't exist on cloud '{name}'".format(name=self.name,
|
|
|
|
port_number=port_number))
|
|
|
|
|
|
|
|
if port_number not in self._nios:
|
|
|
|
raise NodeError("Port {} is not connected".format(port_number))
|
|
|
|
|
|
|
|
nio = self._nios[port_number]
|
|
|
|
nio.stopPacketCapture()
|
|
|
|
bridge_name = "{}-{}".format(self._id, port_number)
|
|
|
|
yield from self._ubridge_send("bridge stop_capture {name}".format(name=bridge_name))
|
|
|
|
|
|
|
|
log.info("Cloud'{name}' [{id}]: stopping packet capture on port {port_number}".format(name=self.name,
|
2016-06-07 13:34:04 +00:00
|
|
|
id=self.id,
|
|
|
|
port_number=port_number))
|