2015-01-14 03:26:32 +02: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 19:52:02 +02:00
|
|
|
"""
|
2015-01-21 04:02:22 +02:00
|
|
|
VPCS VM management (creates command line, processes, files etc.) in
|
2015-04-08 20:17:34 +03:00
|
|
|
order to run a VPCS VM.
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2016-10-19 17:55:33 +03:00
|
|
|
import socket
|
2015-01-14 19:52:02 +02:00
|
|
|
import subprocess
|
|
|
|
import signal
|
|
|
|
import re
|
|
|
|
import asyncio
|
2015-01-16 21:23:43 +02:00
|
|
|
import shutil
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2016-06-23 04:40:46 +03:00
|
|
|
from gns3server.utils.asyncio import wait_for_process_termination
|
|
|
|
from gns3server.utils.asyncio import monitor_process
|
|
|
|
from gns3server.utils.asyncio import subprocess_check_output
|
2016-05-02 18:13:23 +03:00
|
|
|
from gns3server.utils import parse_version
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2015-01-14 19:52:02 +02:00
|
|
|
from .vpcs_error import VPCSError
|
2015-01-19 01:26:56 +02:00
|
|
|
from ..adapters.ethernet_adapter import EthernetAdapter
|
2015-02-24 04:00:34 +02:00
|
|
|
from ..nios.nio_udp import NIOUDP
|
2016-05-11 20:35:36 +03:00
|
|
|
from ..base_node import BaseNode
|
2015-01-23 03:04:24 +02:00
|
|
|
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2015-01-14 19:52:02 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-01-19 00:41:53 +02:00
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
class VPCSVM(BaseNode):
|
2015-01-21 12:33:24 +02:00
|
|
|
module_name = 'vpcs'
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
2015-04-08 20:17:34 +03:00
|
|
|
VPCS VM implementation.
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-04-08 20:17:34 +03:00
|
|
|
:param name: VPCS VM name
|
2016-05-11 20:35:36 +03:00
|
|
|
:param node_id: Node identifier
|
2015-01-20 13:46:15 +02:00
|
|
|
:param project: Project instance
|
2015-04-08 20:17:34 +03:00
|
|
|
:param manager: Manager instance
|
2015-01-14 19:52:02 +02:00
|
|
|
:param console: TCP console port
|
2015-04-08 20:17:34 +03:00
|
|
|
:param startup_script: content of the startup script file
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2018-03-24 14:11:21 +03:00
|
|
|
def __init__(self, name, node_id, project, manager, console=None, console_type="telnet", startup_script=None):
|
2015-01-19 00:41:53 +02:00
|
|
|
|
2018-03-24 14:11:21 +03:00
|
|
|
super().__init__(name, node_id, project, manager, console=console, console_type=console_type, wrap_console=True)
|
2015-01-14 19:52:02 +02:00
|
|
|
self._process = None
|
|
|
|
self._vpcs_stdout_file = ""
|
2015-11-09 11:25:20 +02:00
|
|
|
self._vpcs_version = None
|
2015-01-14 19:52:02 +02:00
|
|
|
self._started = False
|
2016-06-23 04:40:46 +03:00
|
|
|
self._local_udp_tunnel = None
|
2015-01-14 19:52:02 +02:00
|
|
|
|
|
|
|
# VPCS settings
|
2017-01-09 18:07:42 +02:00
|
|
|
if startup_script is not None and not self.script_file: # We disallow override at startup
|
2015-01-21 17:43:34 +02:00
|
|
|
self.startup_script = startup_script
|
2015-01-14 19:52:02 +02:00
|
|
|
self._ethernet_adapter = EthernetAdapter() # one adapter with 1 Ethernet interface
|
|
|
|
|
2017-06-30 11:22:30 +03:00
|
|
|
@property
|
|
|
|
def ethernet_adapter(self):
|
|
|
|
return self._ethernet_adapter
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def close(self):
|
2015-04-08 20:17:34 +03:00
|
|
|
"""
|
|
|
|
Closes this VPCS VM.
|
|
|
|
"""
|
2015-01-23 04:06:17 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
if not (await super().close()):
|
2016-02-29 11:38:30 +02:00
|
|
|
return False
|
2015-01-20 17:24:46 +02:00
|
|
|
|
2015-02-24 04:00:34 +02:00
|
|
|
nio = self._ethernet_adapter.get_nio(0)
|
|
|
|
if isinstance(nio, NIOUDP):
|
2015-03-22 01:19:12 +02:00
|
|
|
self.manager.port_manager.release_udp_port(nio.lport, self._project)
|
2015-02-24 04:00:34 +02:00
|
|
|
|
2016-06-23 04:40:46 +03:00
|
|
|
if self._local_udp_tunnel:
|
|
|
|
self.manager.port_manager.release_udp_port(self._local_udp_tunnel[0].lport, self._project)
|
|
|
|
self.manager.port_manager.release_udp_port(self._local_udp_tunnel[1].lport, self._project)
|
|
|
|
self._local_udp_tunnel = None
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._stop_ubridge()
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2015-03-25 22:43:51 +02:00
|
|
|
if self.is_running():
|
|
|
|
self._terminate_process()
|
2015-02-24 04:00:34 +02:00
|
|
|
|
2016-02-29 11:38:30 +02:00
|
|
|
return True
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _check_requirements(self):
|
2015-01-15 14:02:43 +02:00
|
|
|
"""
|
2015-04-08 20:17:34 +03:00
|
|
|
Check if VPCS is available with the correct version.
|
2015-01-15 14:02:43 +02:00
|
|
|
"""
|
2015-04-08 20:17:34 +03:00
|
|
|
|
2017-02-20 11:56:48 +02:00
|
|
|
path = self._vpcs_path()
|
2015-02-05 13:00:34 +02:00
|
|
|
if not path:
|
2015-01-14 19:52:02 +02:00
|
|
|
raise VPCSError("No path to a VPCS executable has been set")
|
|
|
|
|
2016-06-23 13:10:18 +03:00
|
|
|
# This raise an error if ubridge is not available
|
2017-02-02 20:13:47 +02:00
|
|
|
self.ubridge_path
|
2016-06-23 13:10:18 +03:00
|
|
|
|
2015-02-05 13:00:34 +02:00
|
|
|
if not os.path.isfile(path):
|
|
|
|
raise VPCSError("VPCS program '{}' is not accessible".format(path))
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-02-05 13:00:34 +02:00
|
|
|
if not os.access(path, os.X_OK):
|
|
|
|
raise VPCSError("VPCS program '{}' is not executable".format(path))
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._check_vpcs_version()
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-01-20 20:56:18 +02:00
|
|
|
def __json__(self):
|
|
|
|
|
2015-01-22 00:21:15 +02:00
|
|
|
return {"name": self.name,
|
2016-05-11 20:35:36 +03:00
|
|
|
"node_id": self.id,
|
2017-10-02 11:41:57 +03:00
|
|
|
"node_directory": self.working_path,
|
2015-03-04 17:01:56 +02:00
|
|
|
"status": self.status,
|
2015-01-20 21:54:46 +02:00
|
|
|
"console": self._console,
|
2018-03-24 14:11:21 +03:00
|
|
|
"console_type": self._console_type,
|
2015-02-04 22:48:29 +02:00
|
|
|
"project_id": self.project.id,
|
2016-02-02 19:25:17 +02:00
|
|
|
"command_line": self.command_line}
|
2015-02-27 14:36:11 +02:00
|
|
|
|
2017-02-20 11:56:48 +02:00
|
|
|
def _vpcs_path(self):
|
2015-01-23 03:04:24 +02:00
|
|
|
"""
|
|
|
|
Returns the VPCS executable path.
|
|
|
|
|
|
|
|
:returns: path to VPCS
|
|
|
|
"""
|
|
|
|
|
2019-03-06 18:15:05 +02:00
|
|
|
vpcs_path = self._manager.config.get_section_config("VPCS").get("vpcs_path", "vpcs")
|
|
|
|
if not os.path.isabs(vpcs_path):
|
|
|
|
vpcs_path = shutil.which(vpcs_path)
|
|
|
|
return vpcs_path
|
2015-01-23 03:04:24 +02:00
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
@BaseNode.name.setter
|
2015-01-14 19:52:02 +02:00
|
|
|
def name(self, new_name):
|
|
|
|
"""
|
2015-04-08 20:17:34 +03:00
|
|
|
Sets the name of this VPCS VM.
|
2015-01-14 19:52:02 +02:00
|
|
|
|
|
|
|
:param new_name: name
|
|
|
|
"""
|
|
|
|
|
2015-02-05 15:20:01 +02:00
|
|
|
if self.script_file:
|
2015-01-21 22:46:16 +02:00
|
|
|
content = self.startup_script
|
|
|
|
content = content.replace(self._name, new_name)
|
2017-07-20 18:29:42 +03:00
|
|
|
escaped_name = new_name.replace('\\', '')
|
2017-06-08 16:23:18 +03:00
|
|
|
content = re.sub(r"^set pcname .+$", "set pcname " + escaped_name, content, flags=re.MULTILINE)
|
2015-01-21 22:46:16 +02:00
|
|
|
self.startup_script = content
|
|
|
|
|
|
|
|
super(VPCSVM, VPCSVM).name.__set__(self, new_name)
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-01-21 17:43:34 +02:00
|
|
|
@property
|
|
|
|
def startup_script(self):
|
2015-04-08 20:17:34 +03:00
|
|
|
"""
|
|
|
|
Returns the content of the current startup script
|
|
|
|
"""
|
2015-01-30 15:57:25 +02:00
|
|
|
|
2015-02-05 15:20:01 +02:00
|
|
|
script_file = self.script_file
|
|
|
|
if script_file is None:
|
|
|
|
return None
|
2015-01-30 15:57:25 +02:00
|
|
|
|
2015-01-21 17:43:34 +02:00
|
|
|
try:
|
2015-04-25 20:58:34 +03:00
|
|
|
with open(script_file, "rb") as f:
|
|
|
|
return f.read().decode("utf-8", errors="replace")
|
2015-01-21 17:43:34 +02:00
|
|
|
except OSError as e:
|
2015-04-08 20:17:34 +03:00
|
|
|
raise VPCSError('Cannot read the startup script file "{}": {}'.format(script_file, e))
|
2015-01-21 17:43:34 +02:00
|
|
|
|
|
|
|
@startup_script.setter
|
|
|
|
def startup_script(self, startup_script):
|
|
|
|
"""
|
2015-04-08 20:17:34 +03:00
|
|
|
Updates the startup script.
|
2015-01-21 17:43:34 +02:00
|
|
|
|
2015-04-08 20:17:34 +03:00
|
|
|
:param startup_script: content of the startup script
|
2015-01-21 17:43:34 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-05-05 06:54:56 +03:00
|
|
|
startup_script_path = os.path.join(self.working_dir, 'startup.vpc')
|
|
|
|
with open(startup_script_path, "w+", encoding='utf-8') as f:
|
2015-01-21 22:46:16 +02:00
|
|
|
if startup_script is None:
|
2015-05-05 06:54:56 +03:00
|
|
|
f.write('')
|
2015-01-21 22:46:16 +02:00
|
|
|
else:
|
2015-02-03 22:48:20 +02:00
|
|
|
startup_script = startup_script.replace("%h", self._name)
|
2015-05-05 06:54:56 +03:00
|
|
|
f.write(startup_script)
|
2015-01-21 17:43:34 +02:00
|
|
|
except OSError as e:
|
2015-05-05 06:54:56 +03:00
|
|
|
raise VPCSError('Cannot write the startup script file "{}": {}'.format(startup_script_path, e))
|
2015-01-21 17:43:34 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _check_vpcs_version(self):
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
2015-11-11 22:57:58 +02:00
|
|
|
Checks if the VPCS executable version is >= 0.8b or == 0.6.1.
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
output = await subprocess_check_output(self._vpcs_path(), "-v", cwd=self.working_dir)
|
2019-01-17 13:01:58 +02:00
|
|
|
match = re.search(r"Welcome to Virtual PC Simulator, version ([0-9a-z\.]+)", output)
|
2015-01-14 19:52:02 +02:00
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
2015-11-09 11:25:20 +02:00
|
|
|
self._vpcs_version = parse_version(version)
|
2017-01-23 22:11:52 +02:00
|
|
|
if self._vpcs_version < parse_version("0.6.1"):
|
|
|
|
raise VPCSError("VPCS executable version must be >= 0.6.1 but not a 0.8")
|
2015-01-14 19:52:02 +02:00
|
|
|
else:
|
2017-02-20 11:56:48 +02:00
|
|
|
raise VPCSError("Could not determine the VPCS version for {}".format(self._vpcs_path()))
|
2015-01-14 19:52:02 +02:00
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
|
|
|
raise VPCSError("Error while looking for the VPCS version: {}".format(e))
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def start(self):
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
|
|
|
Starts the VPCS process.
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._check_requirements()
|
2015-01-14 19:52:02 +02:00
|
|
|
if not self.is_running():
|
2016-06-23 04:40:46 +03:00
|
|
|
nio = self._ethernet_adapter.get_nio(0)
|
2016-02-02 19:25:17 +02:00
|
|
|
command = self._build_command()
|
2015-01-14 19:52:02 +02:00
|
|
|
try:
|
2016-02-02 19:25:17 +02:00
|
|
|
log.info("Starting VPCS: {}".format(command))
|
2015-01-20 15:31:47 +02:00
|
|
|
self._vpcs_stdout_file = os.path.join(self.working_dir, "vpcs.log")
|
2015-01-20 17:24:46 +02:00
|
|
|
log.info("Logging to {}".format(self._vpcs_stdout_file))
|
2015-01-14 19:52:02 +02:00
|
|
|
flags = 0
|
|
|
|
if sys.platform.startswith("win32"):
|
|
|
|
flags = subprocess.CREATE_NEW_PROCESS_GROUP
|
2015-04-25 20:58:34 +03:00
|
|
|
with open(self._vpcs_stdout_file, "w", encoding="utf-8") as fd:
|
2016-02-02 19:25:17 +02:00
|
|
|
self.command_line = ' '.join(command)
|
2018-10-15 13:05:49 +03:00
|
|
|
self._process = await asyncio.create_subprocess_exec(*command,
|
2015-01-19 23:43:35 +02:00
|
|
|
stdout=fd,
|
|
|
|
stderr=subprocess.STDOUT,
|
2015-01-20 15:31:47 +02:00
|
|
|
cwd=self.working_dir,
|
2015-01-19 23:43:35 +02:00
|
|
|
creationflags=flags)
|
2015-03-04 17:01:56 +02:00
|
|
|
monitor_process(self._process, self._termination_callback)
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._start_ubridge()
|
2017-07-11 14:42:47 +03:00
|
|
|
if nio:
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.add_ubridge_udp_connection("VPCS-{}".format(self._id), self._local_udp_tunnel[1], nio)
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.start_wrap_console()
|
2016-11-08 20:44:12 +02:00
|
|
|
|
2015-01-20 03:30:57 +02:00
|
|
|
log.info("VPCS instance {} started PID={}".format(self.name, self._process.pid))
|
2015-01-14 19:52:02 +02:00
|
|
|
self._started = True
|
2015-03-04 17:01:56 +02:00
|
|
|
self.status = "started"
|
2015-01-14 19:52:02 +02:00
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
|
|
|
vpcs_stdout = self.read_vpcs_stdout()
|
2017-02-20 11:56:48 +02:00
|
|
|
log.error("Could not start VPCS {}: {}\n{}".format(self._vpcs_path(), e, vpcs_stdout))
|
|
|
|
raise VPCSError("Could not start VPCS {}: {}\n{}".format(self._vpcs_path(), e, vpcs_stdout))
|
2015-01-20 17:24:46 +02:00
|
|
|
|
2021-01-27 12:33:19 +02:00
|
|
|
async def _termination_callback(self, returncode):
|
2015-03-04 17:01:56 +02:00
|
|
|
"""
|
2015-05-13 22:53:42 +03:00
|
|
|
Called when the process has stopped.
|
2015-03-04 17:01:56 +02:00
|
|
|
|
|
|
|
:param returncode: Process returncode
|
|
|
|
"""
|
2018-03-15 09:17:39 +02:00
|
|
|
|
2015-03-04 17:01:56 +02:00
|
|
|
if self._started:
|
2015-05-13 22:53:42 +03:00
|
|
|
log.info("VPCS process has stopped, return code: %d", returncode)
|
2015-03-04 17:01:56 +02:00
|
|
|
self._started = False
|
|
|
|
self.status = "stopped"
|
|
|
|
self._process = None
|
2021-02-14 05:20:10 +02:00
|
|
|
await self._stop_ubridge()
|
|
|
|
await super().stop()
|
2015-07-04 23:08:03 +03:00
|
|
|
if returncode != 0:
|
|
|
|
self.project.emit("log.error", {"message": "VPCS process has stopped, return code: {}\n{}".format(returncode, self.read_vpcs_stdout())})
|
2015-03-04 17:01:56 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def stop(self):
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
|
|
|
Stops the VPCS process.
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._stop_ubridge()
|
2015-01-14 19:52:02 +02:00
|
|
|
if self.is_running():
|
2015-02-09 22:41:31 +02:00
|
|
|
self._terminate_process()
|
2015-04-26 21:57:06 +03:00
|
|
|
if self._process.returncode is None:
|
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
await wait_for_process_termination(self._process, timeout=3)
|
2015-04-26 21:57:06 +03:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
if self._process.returncode is None:
|
2015-05-27 18:21:15 +03:00
|
|
|
try:
|
|
|
|
self._process.kill()
|
|
|
|
except OSError as e:
|
2015-09-04 01:18:27 +03:00
|
|
|
log.error("Cannot stop the VPCS process: {}".format(e))
|
|
|
|
if self._process.returncode is None:
|
2018-03-15 09:17:39 +02:00
|
|
|
log.warning('VPCS VM "{}" with PID={} is still running'.format(self._name, self._process.pid))
|
2015-01-14 19:52:02 +02:00
|
|
|
|
|
|
|
self._process = None
|
|
|
|
self._started = False
|
2018-10-15 13:05:49 +03:00
|
|
|
await super().stop()
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def reload(self):
|
2015-01-22 11:55:11 +02:00
|
|
|
"""
|
2015-04-08 20:17:34 +03:00
|
|
|
Reloads the VPCS process (stop & start).
|
2015-01-22 11:55:11 +02:00
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.stop()
|
|
|
|
await self.start()
|
2015-01-22 11:55:11 +02:00
|
|
|
|
2015-02-09 22:41:31 +02:00
|
|
|
def _terminate_process(self):
|
2015-04-08 20:17:34 +03:00
|
|
|
"""
|
|
|
|
Terminate the process if running
|
|
|
|
"""
|
2015-01-20 17:24:46 +02:00
|
|
|
|
2015-03-25 22:43:51 +02:00
|
|
|
log.info("Stopping VPCS instance {} PID={}".format(self.name, self._process.pid))
|
|
|
|
if sys.platform.startswith("win32"):
|
2018-04-27 13:00:28 +03:00
|
|
|
try:
|
|
|
|
self._process.send_signal(signal.CTRL_BREAK_EVENT)
|
2018-09-28 16:04:38 +03:00
|
|
|
except (SystemError, OSError):
|
2018-04-27 13:00:28 +03:00
|
|
|
pass
|
2015-03-25 22:43:51 +02:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self._process.terminate()
|
2015-04-08 20:17:34 +03:00
|
|
|
# Sometime the process may already be dead when we garbage collect
|
2015-03-25 22:43:51 +02:00
|
|
|
except ProcessLookupError:
|
|
|
|
pass
|
2015-01-20 17:24:46 +02:00
|
|
|
|
2015-01-14 19:52:02 +02: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-04-08 20:17:34 +03:00
|
|
|
|
2015-01-14 19:52:02 +02:00
|
|
|
output = ""
|
|
|
|
if self._vpcs_stdout_file:
|
|
|
|
try:
|
2015-04-25 20:58:34 +03:00
|
|
|
with open(self._vpcs_stdout_file, "rb") as file:
|
|
|
|
output = file.read().decode("utf-8", errors="replace")
|
2015-01-14 19:52:02 +02:00
|
|
|
except OSError as e:
|
2018-03-15 09:17:39 +02:00
|
|
|
log.warning("Could not read {}: {}".format(self._vpcs_stdout_file, e))
|
2015-01-14 19:52:02 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
"""
|
|
|
|
Checks if the VPCS process is running
|
|
|
|
|
|
|
|
:returns: True or False
|
|
|
|
"""
|
|
|
|
|
2015-03-25 22:43:51 +02:00
|
|
|
if self._process and self._process.returncode is None:
|
2015-01-14 19:52:02 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2022-12-30 14:54:37 +02:00
|
|
|
async def reset_console(self):
|
|
|
|
"""
|
|
|
|
Reset console
|
|
|
|
"""
|
|
|
|
|
|
|
|
await self.stop_wrap_console()
|
|
|
|
if self.is_running():
|
|
|
|
await self.start_wrap_console()
|
|
|
|
|
2018-03-24 14:11:21 +03:00
|
|
|
@BaseNode.console_type.setter
|
|
|
|
def console_type(self, new_console_type):
|
|
|
|
"""
|
|
|
|
Sets the console type for this VPCS VM.
|
|
|
|
|
|
|
|
:param new_console_type: console type (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.is_running() and self.console_type != new_console_type:
|
|
|
|
raise VPCSError('"{name}" must be stopped to change the console type to {new_console_type}'.format(name=self._name, new_console_type=new_console_type))
|
|
|
|
|
|
|
|
super(VPCSVM, VPCSVM).console_type.__set__(self, new_console_type)
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def port_add_nio_binding(self, port_number, nio):
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
|
|
|
Adds a port NIO binding.
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
:param port_number: port number
|
2015-01-14 19:52:02 +02:00
|
|
|
:param nio: NIO instance to add to the slot/port
|
|
|
|
"""
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
if not self._ethernet_adapter.port_exists(port_number):
|
2018-10-27 10:47:17 +03:00
|
|
|
raise VPCSError("Port {port_number} doesn't exist on adapter {adapter}".format(adapter=self._ethernet_adapter,
|
2015-01-31 21:01:23 +02:00
|
|
|
port_number=port_number))
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2017-07-12 10:55:23 +03:00
|
|
|
if self.is_running():
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.add_ubridge_udp_connection("VPCS-{}".format(self._id), self._local_udp_tunnel[1], nio)
|
2016-06-25 03:35:39 +03:00
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
self._ethernet_adapter.add_nio(port_number, nio)
|
2015-04-08 20:17:34 +03:00
|
|
|
log.info('VPCS "{name}" [{id}]: {nio} added to port {port_number}'.format(name=self._name,
|
|
|
|
id=self.id,
|
|
|
|
nio=nio,
|
|
|
|
port_number=port_number))
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2015-01-16 17:20:10 +02:00
|
|
|
return nio
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def port_update_nio_binding(self, port_number, nio):
|
2018-10-27 10:47:17 +03:00
|
|
|
"""
|
|
|
|
Updates a port NIO binding.
|
|
|
|
|
|
|
|
:param port_number: port number
|
|
|
|
:param nio: NIO instance to update on the slot/port
|
|
|
|
"""
|
|
|
|
|
2017-06-30 11:22:30 +03:00
|
|
|
if not self._ethernet_adapter.port_exists(port_number):
|
2018-10-27 10:47:17 +03:00
|
|
|
raise VPCSError("Port {port_number} doesn't exist on adapter {adapter}".format(adapter=self._ethernet_adapter,
|
2017-06-30 11:22:30 +03:00
|
|
|
port_number=port_number))
|
2017-07-12 10:55:23 +03:00
|
|
|
if self.is_running():
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.update_ubridge_udp_connection("VPCS-{}".format(self._id), self._local_udp_tunnel[1], nio)
|
2017-06-30 11:22:30 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def port_remove_nio_binding(self, port_number):
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
|
|
|
Removes a port NIO binding.
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
:param port_number: port number
|
2015-01-14 19:52:02 +02:00
|
|
|
|
|
|
|
:returns: NIO instance
|
|
|
|
"""
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
if not self._ethernet_adapter.port_exists(port_number):
|
2018-10-27 10:47:17 +03:00
|
|
|
raise VPCSError("Port {port_number} doesn't exist on adapter {adapter}".format(adapter=self._ethernet_adapter,
|
2015-01-31 21:01:23 +02:00
|
|
|
port_number=port_number))
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2019-04-01 15:47:31 +03:00
|
|
|
await self.stop_capture(port_number)
|
2017-07-12 10:55:23 +03:00
|
|
|
if self.is_running():
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._ubridge_send("bridge delete {name}".format(name="VPCS-{}".format(self._id)))
|
2016-06-25 03:35:39 +03:00
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
nio = self._ethernet_adapter.get_nio(port_number)
|
2015-02-24 04:00:34 +02:00
|
|
|
if isinstance(nio, NIOUDP):
|
2015-03-22 01:19:12 +02:00
|
|
|
self.manager.port_manager.release_udp_port(nio.lport, self._project)
|
2015-01-31 21:01:23 +02:00
|
|
|
self._ethernet_adapter.remove_nio(port_number)
|
2015-01-25 00:32:58 +02:00
|
|
|
|
2015-04-08 20:17:34 +03:00
|
|
|
log.info('VPCS "{name}" [{id}]: {nio} removed from port {port_number}'.format(name=self._name,
|
|
|
|
id=self.id,
|
|
|
|
nio=nio,
|
|
|
|
port_number=port_number))
|
2015-01-14 19:52:02 +02:00
|
|
|
return nio
|
|
|
|
|
2018-10-27 10:47:17 +03:00
|
|
|
def get_nio(self, port_number):
|
2016-06-23 04:40:46 +03:00
|
|
|
"""
|
2018-10-27 10:47:17 +03:00
|
|
|
Gets a port NIO binding.
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2016-06-24 01:56:06 +03:00
|
|
|
:param port_number: port number
|
2018-10-27 10:47:17 +03:00
|
|
|
|
|
|
|
:returns: NIO instance
|
2016-06-23 04:40:46 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not self._ethernet_adapter.port_exists(port_number):
|
2018-10-27 10:47:17 +03:00
|
|
|
raise VPCSError("Port {port_number} doesn't exist on adapter {adapter}".format(adapter=self._ethernet_adapter,
|
2016-06-23 04:40:46 +03:00
|
|
|
port_number=port_number))
|
2018-10-27 10:47:17 +03:00
|
|
|
nio = self._ethernet_adapter.get_nio(port_number)
|
2016-06-23 04:40:46 +03:00
|
|
|
if not nio:
|
|
|
|
raise VPCSError("Port {} is not connected".format(port_number))
|
2018-10-27 10:47:17 +03:00
|
|
|
return nio
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2018-10-27 10:47:17 +03:00
|
|
|
async def start_capture(self, port_number, output_file):
|
|
|
|
"""
|
|
|
|
Starts a packet capture.
|
|
|
|
|
|
|
|
:param port_number: port number
|
|
|
|
:param output_file: PCAP destination file for the capture
|
|
|
|
"""
|
|
|
|
|
|
|
|
nio = self.get_nio(port_number)
|
2016-06-23 04:40:46 +03:00
|
|
|
if nio.capturing:
|
2018-10-27 10:47:17 +03:00
|
|
|
raise VPCSError("Packet capture is already active on port {port_number}".format(port_number=port_number))
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2019-04-01 16:58:18 +03:00
|
|
|
nio.start_packet_capture(output_file)
|
2016-12-14 13:01:34 +02:00
|
|
|
if self.ubridge:
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._ubridge_send('bridge start_capture {name} "{output_file}"'.format(name="VPCS-{}".format(self._id),
|
2016-06-23 04:40:46 +03:00
|
|
|
output_file=output_file))
|
|
|
|
|
|
|
|
log.info("VPCS '{name}' [{id}]: starting packet capture on port {port_number}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
port_number=port_number))
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def stop_capture(self, port_number):
|
2016-06-23 04:40:46 +03:00
|
|
|
"""
|
|
|
|
Stops a packet capture.
|
|
|
|
|
|
|
|
:param port_number: port number
|
|
|
|
"""
|
|
|
|
|
2018-10-27 10:47:17 +03:00
|
|
|
nio = self.get_nio(port_number)
|
2019-04-01 15:47:31 +03:00
|
|
|
if not nio.capturing:
|
|
|
|
return
|
2016-06-23 04:40:46 +03:00
|
|
|
|
2019-04-01 16:58:18 +03:00
|
|
|
nio.stop_packet_capture()
|
2016-12-14 13:01:34 +02:00
|
|
|
if self.ubridge:
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._ubridge_send('bridge stop_capture {name}'.format(name="VPCS-{}".format(self._id)))
|
2016-06-23 04:40:46 +03:00
|
|
|
|
|
|
|
log.info("VPCS '{name}' [{id}]: stopping packet capture on port {port_number}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
port_number=port_number))
|
|
|
|
|
2015-01-14 19:52:02 +02:00
|
|
|
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 14:12:26 +02:00
|
|
|
-d vm device name, works only when -i is set to 1
|
2015-01-14 19:52:02 +02: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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2017-02-20 11:56:48 +02:00
|
|
|
command = [self._vpcs_path()]
|
2016-11-08 20:44:12 +02:00
|
|
|
command.extend(["-p", str(self._internal_console_port)]) # listen to console port
|
2015-05-26 04:07:12 +03:00
|
|
|
command.extend(["-m", str(self._manager.get_mac_id(self.id))]) # the unique ID is used to set the MAC address offset
|
|
|
|
command.extend(["-i", "1"]) # option to start only one VPC instance
|
|
|
|
command.extend(["-F"]) # option to avoid the daemonization of VPCS
|
2016-05-22 02:42:34 +03:00
|
|
|
if self._vpcs_version >= parse_version("0.8b"):
|
|
|
|
command.extend(["-R"]) # disable the relay feature of VPCS (starting with VPCS 0.8)
|
|
|
|
else:
|
2018-03-15 09:17:39 +02:00
|
|
|
log.warning("The VPCS relay feature could not be disabled because the VPCS version is below 0.8b")
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2017-07-11 14:42:47 +03:00
|
|
|
# use the local UDP tunnel to uBridge instead
|
|
|
|
if not self._local_udp_tunnel:
|
|
|
|
self._local_udp_tunnel = self._create_local_udp_tunnel()
|
|
|
|
nio = self._local_udp_tunnel[0]
|
2018-03-15 09:17:39 +02:00
|
|
|
if nio and isinstance(nio, NIOUDP):
|
|
|
|
# UDP tunnel
|
|
|
|
command.extend(["-s", str(nio.lport)]) # source UDP port
|
|
|
|
command.extend(["-c", str(nio.rport)]) # destination UDP port
|
|
|
|
try:
|
|
|
|
command.extend(["-t", socket.gethostbyname(nio.rhost)]) # destination host, we need to resolve the hostname because VPCS doesn't support it
|
|
|
|
except socket.gaierror as e:
|
|
|
|
raise VPCSError("Can't resolve hostname {}".format(nio.rhost))
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-02-05 15:20:01 +02:00
|
|
|
if self.script_file:
|
2015-04-28 07:23:27 +03:00
|
|
|
command.extend([os.path.basename(self.script_file)])
|
2015-01-14 19:52:02 +02:00
|
|
|
return command
|
|
|
|
|
|
|
|
@property
|
|
|
|
def script_file(self):
|
|
|
|
"""
|
2015-04-08 20:17:34 +03:00
|
|
|
Returns the startup script file for this VPCS VM.
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-04-08 20:17:34 +03:00
|
|
|
:returns: path to startup script file
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
|
|
|
|
2015-04-08 20:17:34 +03:00
|
|
|
# use the default VPCS file if it exists
|
2015-02-05 15:20:01 +02:00
|
|
|
path = os.path.join(self.working_dir, 'startup.vpc')
|
|
|
|
if os.path.exists(path):
|
|
|
|
return path
|
|
|
|
else:
|
|
|
|
return None
|