2015-01-14 01:26:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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/>.
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
"""
|
2015-01-20 12:12:26 +00:00
|
|
|
VPCS vm management (creates command line, processes, files etc.) in
|
2015-01-14 17:52:02 +00:00
|
|
|
order to run an VPCS instance.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import signal
|
|
|
|
import re
|
|
|
|
import asyncio
|
2015-01-16 15:20:10 +00:00
|
|
|
import socket
|
2015-01-16 19:23:43 +00:00
|
|
|
import shutil
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
|
|
from pkg_resources import parse_version
|
|
|
|
from .vpcs_error import VPCSError
|
2015-01-18 23:26:56 +00:00
|
|
|
from ..adapters.ethernet_adapter import EthernetAdapter
|
|
|
|
from ..nios.nio_udp import NIO_UDP
|
|
|
|
from ..nios.nio_tap import NIO_TAP
|
2015-01-16 15:20:10 +00:00
|
|
|
from ..attic import has_privileged_access
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
|
|
from ..base_vm import BaseVM
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-18 22:41:53 +00:00
|
|
|
|
2015-01-20 12:12:26 +00:00
|
|
|
class VPCSVM(BaseVM):
|
2015-01-20 12:24:00 +00:00
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
"""
|
2015-01-20 12:12:26 +00:00
|
|
|
VPCS vm implementation.
|
2015-01-14 17:52:02 +00:00
|
|
|
|
2015-01-20 12:12:26 +00:00
|
|
|
:param name: name of this VPCS vm
|
2015-01-20 01:30:57 +00:00
|
|
|
:param uuid: VPCS instance UUID
|
2015-01-20 11:46:15 +00:00
|
|
|
:param project: Project instance
|
2015-01-19 10:22:24 +00:00
|
|
|
:param manager: parent VM Manager
|
2015-01-14 17:52:02 +00:00
|
|
|
:param console: TCP console port
|
2015-01-20 18:56:18 +00:00
|
|
|
:param script_file: A VPCS startup script
|
2015-01-14 17:52:02 +00:00
|
|
|
"""
|
2015-01-20 12:24:00 +00:00
|
|
|
|
2015-01-20 18:56:18 +00:00
|
|
|
def __init__(self, name, uuid, project, manager, console=None, script_file=None):
|
2015-01-18 22:41:53 +00:00
|
|
|
|
2015-01-20 11:46:15 +00:00
|
|
|
super().__init__(name, uuid, project, manager)
|
2015-01-19 10:22:24 +00:00
|
|
|
|
2015-01-16 19:23:43 +00:00
|
|
|
self._path = self._config.get_section_config("VPCS").get("path", "vpcs")
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
|
|
self._console = console
|
2015-01-16 19:23:43 +00:00
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
self._command = []
|
|
|
|
self._process = None
|
|
|
|
self._vpcs_stdout_file = ""
|
|
|
|
self._started = False
|
|
|
|
|
|
|
|
# VPCS settings
|
2015-01-20 18:56:18 +00:00
|
|
|
self._script_file = script_file
|
2015-01-14 17:52:02 +00:00
|
|
|
self._ethernet_adapter = EthernetAdapter() # one adapter with 1 Ethernet interface
|
|
|
|
|
2015-01-20 19:54:46 +00:00
|
|
|
if self._console is not None:
|
|
|
|
self._console = self._manager.port_manager.reserve_console_port(self._console)
|
|
|
|
else:
|
|
|
|
self._console = self._manager.port_manager.get_free_console_port()
|
2015-01-15 15:59:01 +00:00
|
|
|
|
2015-01-20 15:24:46 +00:00
|
|
|
def __del__(self):
|
|
|
|
self._kill_process()
|
|
|
|
|
2015-01-15 12:02:43 +00:00
|
|
|
def _check_requirements(self):
|
|
|
|
"""
|
|
|
|
Check if VPCS is available with the correct version
|
|
|
|
"""
|
2015-01-18 22:41:53 +00:00
|
|
|
|
2015-01-16 19:23:43 +00:00
|
|
|
if self._path == "vpcs":
|
|
|
|
self._path = shutil.which("vpcs")
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
if not self._path:
|
|
|
|
raise VPCSError("No path to a VPCS executable has been set")
|
|
|
|
|
|
|
|
if not os.path.isfile(self._path):
|
|
|
|
raise VPCSError("VPCS program '{}' is not accessible".format(self._path))
|
|
|
|
|
|
|
|
if not os.access(self._path, os.X_OK):
|
|
|
|
raise VPCSError("VPCS program '{}' is not executable".format(self._path))
|
|
|
|
|
2015-01-15 12:02:43 +00:00
|
|
|
self._check_vpcs_version()
|
2015-01-14 17:52:02 +00:00
|
|
|
|
2015-01-20 18:56:18 +00:00
|
|
|
def __json__(self):
|
|
|
|
|
2015-01-20 19:54:46 +00:00
|
|
|
return {"name": self._name,
|
|
|
|
"uuid": self._uuid,
|
|
|
|
"console": self._console,
|
2015-01-20 18:56:18 +00:00
|
|
|
"project_uuid": self.project.uuid,
|
|
|
|
"script_file": self.script_file}
|
|
|
|
|
2015-01-16 19:23:43 +00:00
|
|
|
@property
|
|
|
|
def console(self):
|
|
|
|
"""
|
2015-01-20 12:12:26 +00:00
|
|
|
Returns the console port of this VPCS vm.
|
2015-01-16 19:23:43 +00:00
|
|
|
|
|
|
|
:returns: console port
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._console
|
|
|
|
|
2015-01-20 12:04:20 +00:00
|
|
|
# FIXME: correct way to subclass a property?
|
2015-01-20 01:30:57 +00:00
|
|
|
@BaseVM.name.setter
|
2015-01-14 17:52:02 +00:00
|
|
|
def name(self, new_name):
|
|
|
|
"""
|
2015-01-20 12:12:26 +00:00
|
|
|
Sets the name of this VPCS vm.
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
|
|
:param new_name: name
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._script_file:
|
|
|
|
# update the startup.vpc
|
2015-01-20 13:31:47 +00:00
|
|
|
config_path = os.path.join(self.working_dir, "startup.vpc")
|
2015-01-14 17:52:02 +00:00
|
|
|
if os.path.isfile(config_path):
|
|
|
|
try:
|
|
|
|
with open(config_path, "r+", errors="replace") as f:
|
|
|
|
old_config = f.read()
|
|
|
|
new_config = old_config.replace(self._name, new_name)
|
|
|
|
f.seek(0)
|
|
|
|
f.write(new_config)
|
|
|
|
except OSError as e:
|
|
|
|
raise VPCSError("Could not amend the configuration {}: {}".format(config_path, e))
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
log.info("VPCS {name} [{uuid}]: renamed to {new_name}".format(name=self._name,
|
|
|
|
uuid=self.uuid,
|
|
|
|
new_name=new_name))
|
|
|
|
BaseVM.name = new_name
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
|
|
def _check_vpcs_version(self):
|
|
|
|
"""
|
|
|
|
Checks if the VPCS executable version is >= 0.5b1.
|
|
|
|
"""
|
2015-01-20 12:04:20 +00:00
|
|
|
# TODO: should be async
|
2015-01-14 17:52:02 +00:00
|
|
|
try:
|
2015-01-20 13:31:47 +00:00
|
|
|
output = subprocess.check_output([self._path, "-v"], cwd=self.working_dir)
|
2015-01-14 17:52:02 +00:00
|
|
|
match = re.search("Welcome to Virtual PC Simulator, version ([0-9a-z\.]+)", output.decode("utf-8"))
|
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
|
|
|
if parse_version(version) < parse_version("0.5b1"):
|
|
|
|
raise VPCSError("VPCS executable version must be >= 0.5b1")
|
|
|
|
else:
|
|
|
|
raise VPCSError("Could not determine the VPCS version for {}".format(self._path))
|
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
|
|
|
raise VPCSError("Error while looking for the VPCS version: {}".format(e))
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def start(self):
|
|
|
|
"""
|
|
|
|
Starts the VPCS process.
|
|
|
|
"""
|
|
|
|
|
2015-01-20 21:50:26 +00:00
|
|
|
self._check_requirements()
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
if not self.is_running():
|
2015-01-16 15:20:10 +00:00
|
|
|
if not self._ethernet_adapter.get_nio(0):
|
|
|
|
raise VPCSError("This VPCS instance must be connected in order to start")
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
|
|
self._command = self._build_command()
|
|
|
|
try:
|
2015-01-20 15:24:46 +00:00
|
|
|
log.info("Starting VPCS: {}".format(self._command))
|
2015-01-20 13:31:47 +00:00
|
|
|
self._vpcs_stdout_file = os.path.join(self.working_dir, "vpcs.log")
|
2015-01-20 15:24:46 +00:00
|
|
|
log.info("Logging to {}".format(self._vpcs_stdout_file))
|
2015-01-14 17:52:02 +00:00
|
|
|
flags = 0
|
|
|
|
if sys.platform.startswith("win32"):
|
|
|
|
flags = subprocess.CREATE_NEW_PROCESS_GROUP
|
|
|
|
with open(self._vpcs_stdout_file, "w") as fd:
|
|
|
|
self._process = yield from asyncio.create_subprocess_exec(*self._command,
|
2015-01-19 21:43:35 +00:00
|
|
|
stdout=fd,
|
|
|
|
stderr=subprocess.STDOUT,
|
2015-01-20 13:31:47 +00:00
|
|
|
cwd=self.working_dir,
|
2015-01-19 21:43:35 +00:00
|
|
|
creationflags=flags)
|
2015-01-20 01:30:57 +00:00
|
|
|
log.info("VPCS instance {} started PID={}".format(self.name, self._process.pid))
|
2015-01-14 17:52:02 +00:00
|
|
|
self._started = True
|
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
|
|
|
vpcs_stdout = self.read_vpcs_stdout()
|
2015-01-20 15:24:46 +00:00
|
|
|
log.error("Could not start VPCS {}: {}\n{}".format(self._path, e, vpcs_stdout))
|
|
|
|
raise VPCSError("Could not start VPCS {}: {}\n{}".format(self._path, e, vpcs_stdout))
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stops the VPCS process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# stop the VPCS process
|
|
|
|
if self.is_running():
|
2015-01-20 15:24:46 +00:00
|
|
|
self._kill_process()
|
2015-01-15 13:27:33 +00:00
|
|
|
yield from self._process.wait()
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
|
|
self._process = None
|
|
|
|
self._started = False
|
|
|
|
|
2015-01-20 15:24:46 +00:00
|
|
|
def _kill_process(self):
|
|
|
|
"""Kill the process if running"""
|
|
|
|
|
|
|
|
if self._process:
|
|
|
|
log.info("Stopping VPCS instance {} PID={}".format(self.name, self._process.pid))
|
|
|
|
if sys.platform.startswith("win32"):
|
|
|
|
self._process.send_signal(signal.CTRL_BREAK_EVENT)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self._process.terminate()
|
|
|
|
# Sometime the process can already be dead when we garbage collect
|
|
|
|
except ProcessLookupError:
|
|
|
|
pass
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
def read_vpcs_stdout(self):
|
|
|
|
"""
|
|
|
|
Reads the standard output of the VPCS process.
|
|
|
|
Only use when the process has been stopped or has crashed.
|
|
|
|
"""
|
2015-01-20 12:04:20 +00:00
|
|
|
# TODO: should be async
|
2015-01-14 17:52:02 +00:00
|
|
|
output = ""
|
|
|
|
if self._vpcs_stdout_file:
|
|
|
|
try:
|
|
|
|
with open(self._vpcs_stdout_file, errors="replace") as file:
|
|
|
|
output = file.read()
|
|
|
|
except OSError as e:
|
|
|
|
log.warn("could not read {}: {}".format(self._vpcs_stdout_file, e))
|
|
|
|
return output
|
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
"""
|
|
|
|
Checks if the VPCS process is running
|
|
|
|
|
|
|
|
:returns: True or False
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._process:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2015-01-16 15:20:10 +00:00
|
|
|
def port_add_nio_binding(self, port_id, nio_settings):
|
2015-01-14 17:52:02 +00:00
|
|
|
"""
|
|
|
|
Adds a port NIO binding.
|
|
|
|
|
|
|
|
:param port_id: port ID
|
|
|
|
:param nio: NIO instance to add to the slot/port
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not self._ethernet_adapter.port_exists(port_id):
|
|
|
|
raise VPCSError("Port {port_id} doesn't exist in adapter {adapter}".format(adapter=self._ethernet_adapter,
|
|
|
|
port_id=port_id))
|
|
|
|
|
2015-01-16 15:20:10 +00:00
|
|
|
nio = None
|
|
|
|
if nio_settings["type"] == "nio_udp":
|
|
|
|
lport = nio_settings["lport"]
|
|
|
|
rhost = nio_settings["rhost"]
|
|
|
|
rport = nio_settings["rport"]
|
|
|
|
try:
|
2015-01-20 12:04:20 +00:00
|
|
|
# TODO: handle IPv6
|
2015-01-16 15:20:10 +00:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
|
|
|
sock.connect((rhost, rport))
|
|
|
|
except OSError as e:
|
|
|
|
raise VPCSError("Could not create an UDP connection to {}:{}: {}".format(rhost, rport, e))
|
|
|
|
nio = NIO_UDP(lport, rhost, rport)
|
|
|
|
elif nio_settings["type"] == "nio_tap":
|
2015-01-20 12:12:26 +00:00
|
|
|
tap_vm = nio_settings["tap_device"]
|
2015-01-16 15:20:10 +00:00
|
|
|
if not has_privileged_access(self._path):
|
2015-01-20 12:12:26 +00:00
|
|
|
raise VPCSError("{} has no privileged access to {}.".format(self._path, tap_vm))
|
|
|
|
nio = NIO_TAP(tap_vm)
|
2015-01-16 15:20:10 +00:00
|
|
|
if not nio:
|
|
|
|
raise VPCSError("Requested NIO does not exist or is not supported: {}".format(nio_settings["type"]))
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
self._ethernet_adapter.add_nio(port_id, nio)
|
2015-01-20 01:30:57 +00:00
|
|
|
log.info("VPCS {name} {uuid}]: {nio} added to port {port_id}".format(name=self._name,
|
|
|
|
uuid=self.uuid,
|
|
|
|
nio=nio,
|
|
|
|
port_id=port_id))
|
2015-01-16 15:20:10 +00:00
|
|
|
return nio
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
|
|
def port_remove_nio_binding(self, port_id):
|
|
|
|
"""
|
|
|
|
Removes a port NIO binding.
|
|
|
|
|
|
|
|
:param port_id: port ID
|
|
|
|
|
|
|
|
:returns: NIO instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not self._ethernet_adapter.port_exists(port_id):
|
|
|
|
raise VPCSError("Port {port_id} doesn't exist in adapter {adapter}".format(adapter=self._ethernet_adapter,
|
|
|
|
port_id=port_id))
|
|
|
|
|
|
|
|
nio = self._ethernet_adapter.get_nio(port_id)
|
|
|
|
self._ethernet_adapter.remove_nio(port_id)
|
2015-01-20 01:30:57 +00:00
|
|
|
log.info("VPCS {name} [{uuid}]: {nio} removed from port {port_id}".format(name=self._name,
|
|
|
|
uuid=self.uuid,
|
|
|
|
nio=nio,
|
|
|
|
port_id=port_id))
|
2015-01-14 17:52:02 +00:00
|
|
|
return nio
|
|
|
|
|
|
|
|
def _build_command(self):
|
|
|
|
"""
|
|
|
|
Command to start the VPCS process.
|
|
|
|
(to be passed to subprocess.Popen())
|
|
|
|
|
|
|
|
VPCS command line:
|
|
|
|
usage: vpcs [options] [scriptfile]
|
|
|
|
Option:
|
|
|
|
-h print this help then exit
|
|
|
|
-v print version information then exit
|
|
|
|
|
|
|
|
-i num number of vpc instances to start (default is 9)
|
|
|
|
-p port run as a daemon listening on the tcp 'port'
|
|
|
|
-m num start byte of ether address, default from 0
|
|
|
|
-r file load and execute script file
|
|
|
|
compatible with older versions, DEPRECATED.
|
|
|
|
|
|
|
|
-e tap mode, using /dev/tapx by default (linux only)
|
|
|
|
-u udp mode, default
|
|
|
|
|
|
|
|
udp mode options:
|
|
|
|
-s port local udp base port, default from 20000
|
|
|
|
-c port remote udp base port (dynamips udp port), default from 30000
|
|
|
|
-t ip remote host IP, default 127.0.0.1
|
|
|
|
|
|
|
|
tap mode options:
|
2015-01-20 12:12:26 +00:00
|
|
|
-d vm device name, works only when -i is set to 1
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
|
|
hypervisor mode option:
|
|
|
|
-H port run as the hypervisor listening on the tcp 'port'
|
|
|
|
|
|
|
|
If no 'scriptfile' specified, vpcs will read and execute the file named
|
|
|
|
'startup.vpc' if it exsits in the current directory.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
command = [self._path]
|
|
|
|
command.extend(["-p", str(self._console)]) # listen to console port
|
|
|
|
|
|
|
|
nio = self._ethernet_adapter.get_nio(0)
|
|
|
|
if nio:
|
2015-01-16 15:20:10 +00:00
|
|
|
print(nio)
|
|
|
|
print(isinstance(nio, NIO_UDP))
|
2015-01-14 17:52:02 +00:00
|
|
|
if isinstance(nio, NIO_UDP):
|
|
|
|
# UDP tunnel
|
|
|
|
command.extend(["-s", str(nio.lport)]) # source UDP port
|
|
|
|
command.extend(["-c", str(nio.rport)]) # destination UDP port
|
|
|
|
command.extend(["-t", nio.rhost]) # destination host
|
|
|
|
|
|
|
|
elif isinstance(nio, NIO_TAP):
|
|
|
|
# TAP interface
|
|
|
|
command.extend(["-e"])
|
2015-01-20 12:12:26 +00:00
|
|
|
command.extend(["-d", nio.tap_vm])
|
2015-01-14 17:52:02 +00:00
|
|
|
|
2015-01-20 12:04:20 +00:00
|
|
|
# FIXME: find workaround
|
|
|
|
# command.extend(["-m", str(self._id)]) # the unique ID is used to set the MAC address offset
|
2015-01-14 17:52:02 +00:00
|
|
|
command.extend(["-i", "1"]) # option to start only one VPC instance
|
|
|
|
command.extend(["-F"]) # option to avoid the daemonization of VPCS
|
|
|
|
if self._script_file:
|
|
|
|
command.extend([self._script_file])
|
|
|
|
return command
|
|
|
|
|
|
|
|
@property
|
|
|
|
def script_file(self):
|
|
|
|
"""
|
|
|
|
Returns the script-file for this VPCS instance.
|
|
|
|
|
|
|
|
:returns: path to script-file
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._script_file
|
|
|
|
|
|
|
|
@script_file.setter
|
|
|
|
def script_file(self, script_file):
|
|
|
|
"""
|
|
|
|
Sets the script-file for this VPCS instance.
|
|
|
|
|
|
|
|
:param script_file: path to base-script-file
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._script_file = script_file
|
2015-01-20 01:30:57 +00:00
|
|
|
log.info("VPCS {name} [{uuid}]: script_file set to {config}".format(name=self._name,
|
|
|
|
uuid=self.uuid,
|
|
|
|
config=self._script_file))
|