python/machine: use socketpair() for console socket
PATCH v3 6/6 python/machine: remove unused sock_dir argument
In reply to John SnowBy using a socketpair for all of the sockets managed by the VM class and its extensions, we don't need the sock_dir argument anymore, so remove it.
We only added this argument so that we could specify a second, shorter temporary directory for cases where the temp/log dirs were "too long" as a socket name on macOS. We don't need it for this class now. In one case, avocado testing takes over responsibility for creating an appropriate sockdir.
--- python/qemu/machine/machine.py | 18 ------------------ python/qemu/machine/qtest.py | 5 +---- tests/avocado/acpi-bits.py | 5 +---- tests/avocado/avocado_qemu/__init__.py | 2 +- tests/avocado/machine_aspeed.py | 5 ++++- tests/qemu-iotests/iotests.py | 2 +- tests/qemu-iotests/tests/copy-before-write | 3 +-- 7 files changed, 9 insertions(+), 31 deletions(-)
python/qemu/machine/machine.py +0 -18
@@ -127,7 +127,6 @@ def __init__(self, name: Optional[str] = None, base_temp_dir: str = "/var/tmp", monitor_address: Optional[SocketAddrT] = None, - sock_dir: Optional[str] = None, drain_console: bool = False, console_log: Optional[str] = None, log_dir: Optional[str] = None,
@@ -141,7 +140,6 @@ def __init__(self, @param name: prefix for socket and log file names (default: qemu-PID) @param base_temp_dir: default location where temp files are created @param monitor_address: address for QMP monitor - @param sock_dir: where to create socket (defaults to base_temp_dir) @param drain_console: (optional) True to drain console socket to buffer @param console_log: (optional) path to console log file @param log_dir: where to create and keep log files
@@ -163,7 +161,6 @@ def __init__(self, Tuple[socket.socket, socket.socket]] = None self._temp_dir: Optional[str] = None self._base_temp_dir = base_temp_dir - self._sock_dir = sock_dir self._log_dir = log_dir self._monitor_address = monitor_address
@@ -189,9 +186,6 @@ def __init__(self, self._console_index = 0 self._console_set = False self._console_device_type: Optional[str] = None - self._console_address = os.path.join( - self.sock_dir, f"{self._name}.con" - ) self._console_socket: Optional[socket.socket] = None self._console_file: Optional[socket.SocketIO] = None self._remove_files: List[str] = []
@@ -335,9 +329,6 @@ def args(self) -> List[str]: return self._args def _pre_launch(self) -> None: - if self._console_set: - self._remove_files.append(self._console_address) - if self._qmp_set: if self._monitor_address is None: self._sock_pair = socket.socketpair()
@@ -937,15 +928,6 @@ def temp_dir(self) -> str: dir=self._base_temp_dir) return self._temp_dir - @property - def sock_dir(self) -> str: - """ - Returns the directory used for sockfiles by this machine. - """ - if self._sock_dir: - return self._sock_dir - return self.temp_dir - @property def log_dir(self) -> str: """
python/qemu/machine/qtest.py +1 -4
@@ -135,17 +135,14 @@ def __init__(self, wrapper: Sequence[str] = (), name: Optional[str] = None, base_temp_dir: str = "/var/tmp", - sock_dir: Optional[str] = None, qmp_timer: Optional[float] = None): # pylint: disable=too-many-arguments if name is None: name = "qemu-%d" % os.getpid() - if sock_dir is None: - sock_dir = base_temp_dir super().__init__(binary, args, wrapper=wrapper, name=name, base_temp_dir=base_temp_dir, - sock_dir=sock_dir, qmp_timer=qmp_timer) + qmp_timer=qmp_timer) self._qtest: Optional[QEMUQtestProtocol] = None self._qtest_sock_pair: Optional[ Tuple[socket.socket, socket.socket]] = None
tests/avocado/acpi-bits.py +1 -4
@@ -92,17 +92,14 @@ def __init__(self, base_temp_dir: str = "/var/tmp", debugcon_log: str = "debugcon-log.txt", debugcon_addr: str = "0x403", - sock_dir: Optional[str] = None, qmp_timer: Optional[float] = None): # pylint: disable=too-many-arguments if name is None: name = "qemu-bits-%d" % os.getpid() - if sock_dir is None: - sock_dir = base_temp_dir super().__init__(binary, args, wrapper=wrapper, name=name, base_temp_dir=base_temp_dir, - sock_dir=sock_dir, qmp_timer=qmp_timer) + qmp_timer=qmp_timer) self.debugcon_log = debugcon_log self.debugcon_addr = debugcon_addr self.base_temp_dir = base_temp_dir
tests/avocado/avocado_qemu/__init__.py +1 -1
@@ -322,7 +322,7 @@ def require_multiprocess(self): def _new_vm(self, name, *args): self._sd = tempfile.TemporaryDirectory(prefix="qemu_") vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, - sock_dir=self._sd.name, log_dir=self.logdir) + log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name) self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) self.log.debug('QEMUMachine "%s" log_dir: %s', name, vm.log_dir)
tests/avocado/machine_aspeed.py +4 -1
@@ -247,7 +247,10 @@ def test_arm_ast2600_evb_buildroot_tpm(self): image_path = self.fetch_asset(image_url, asset_hash=image_hash, algorithm='sha256') - socket = os.path.join(self.vm.sock_dir, 'swtpm-socket') + # force creation of VM object, which also defines self._sd + vm = self.vm + + socket = os.path.join(self._sd.name, 'swtpm-socket') subprocess.run(['swtpm', 'socket', '-d', '--tpm2', '--tpmstate', f'dir={self.vm.temp_dir}',
tests/qemu-iotests/iotests.py +1 -1
@@ -823,7 +823,7 @@ def __init__(self, path_suffix=''): super().__init__(qemu_prog, qemu_opts, wrapper=wrapper, name=name, base_temp_dir=test_dir, - sock_dir=sock_dir, qmp_timer=timer) + qmp_timer=timer) self._num_drives = 0 def _post_shutdown(self) -> None:
tests/qemu-iotests/tests/copy-before-write +1 -2
@@ -44,8 +44,7 @@ class TestCbwError(iotests.QMPTestCase): opts = ['-nodefaults', '-display', 'none', '-machine', 'none'] self.vm = QEMUMachine(iotests.qemu_prog, opts, - base_temp_dir=iotests.test_dir, - sock_dir=iotests.sock_dir) + base_temp_dir=iotests.test_dir) self.vm.launch() def do_cbw_error(self, on_cbw_error):
Signature · 1 line
2.41.0
PATCH v3 5/6 python/machine: use socketpair() for qtest connection
In reply to John SnowLike the QMP and console sockets, begin using socketpairs for the qtest connection, too. After this patch, we'll be able to remove the vestigial sock_dir argument, but that cleanup is best done in its own patch.
--- python/qemu/machine/qtest.py | 49 +++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 9 deletions(-)
python/qemu/machine/qtest.py +40 -9
@@ -24,6 +24,7 @@ Optional, Sequence, TextIO, + Tuple, ) from qemu.qmp import SocketAddrT
@@ -38,23 +39,41 @@ class QEMUQtestProtocol: :param address: QEMU address, can be either a unix socket path (string) or a tuple in the form ( address, port ) for a TCP connection - :param server: server mode, listens on the socket (bool) + :param sock: An existing socket can be provided as an alternative to + an address. One of address or sock must be provided. + :param server: server mode, listens on the socket. Only meaningful + in conjunction with an address and not an existing + socket. + :raise socket.error: on socket connection errors .. note:: No connection is established by __init__(), this is done by the connect() or accept() methods. """ - def __init__(self, address: SocketAddrT, + def __init__(self, + address: Optional[SocketAddrT] = None, + sock: Optional[socket.socket] = None, server: bool = False): + if address is None and sock is None: + raise ValueError("Either 'address' or 'sock' must be specified") + if address is not None and sock is not None: + raise ValueError( + "Either 'address' or 'sock' must be specified, but not both") + if sock is not None and server: + raise ValueError("server=True is meaningless when passing socket") + self._address = address - self._sock = self._get_sock() + self._sock = sock or self._get_sock() self._sockfile: Optional[TextIO] = None + if server: + assert self._address is not None self._sock.bind(self._address) self._sock.listen(1) def _get_sock(self) -> socket.socket: + assert self._address is not None if isinstance(self._address, tuple): family = socket.AF_INET else:
@@ -67,7 +86,8 @@ def connect(self) -> None: @raise socket.error on socket connection errors """ - self._sock.connect(self._address) + if self._address is not None: + self._sock.connect(self._address) self._sockfile = self._sock.makefile(mode='r') def accept(self) -> None:
@@ -127,29 +147,40 @@ def __init__(self, base_temp_dir=base_temp_dir, sock_dir=sock_dir, qmp_timer=qmp_timer) self._qtest: Optional[QEMUQtestProtocol] = None - self._qtest_path = os.path.join(sock_dir, name + "-qtest.sock") + self._qtest_sock_pair: Optional[ + Tuple[socket.socket, socket.socket]] = None @property def _base_args(self) -> List[str]: args = super()._base_args + assert self._qtest_sock_pair is not None + fd = self._qtest_sock_pair[0].fileno() args.extend([ - '-qtest', f"unix:path={self._qtest_path}", + '-chardev', f"socket,id=qtest,fd={fd}", + '-qtest', 'chardev:qtest', '-accel', 'qtest' ]) return args def _pre_launch(self) -> None: + self._qtest_sock_pair = socket.socketpair() + os.set_inheritable(self._qtest_sock_pair[0].fileno(), True) super()._pre_launch() - self._qtest = QEMUQtestProtocol(self._qtest_path, server=True) + self._qtest = QEMUQtestProtocol(sock=self._qtest_sock_pair[1]) def _post_launch(self) -> None: assert self._qtest is not None super()._post_launch() - self._qtest.accept() + if self._qtest_sock_pair: + self._qtest_sock_pair[0].close() + self._qtest.connect() def _post_shutdown(self) -> None: + if self._qtest_sock_pair: + self._qtest_sock_pair[0].close() + self._qtest_sock_pair[1].close() + self._qtest_sock_pair = None super()._post_shutdown() - self._remove_if_exists(self._qtest_path) def qtest(self, cmd: str) -> str: """
Signature · 1 line
2.41.0
PATCH v3 3/6 python/console_socket: accept existing FD in initializer
In reply to John SnowUseful if we want to use ConsoleSocket() for a socket created by socketpair().
--- python/qemu/machine/console_socket.py | 29 +++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
python/qemu/machine/console_socket.py +21 -8
@@ -24,19 +24,32 @@ class ConsoleSocket(socket.socket): """ ConsoleSocket represents a socket attached to a char device. - Optionally (if drain==True), drains the socket and places the bytes - into an in memory buffer for later processing. - - Optionally a file path can be passed in and we will also - dump the characters to this file for debugging purposes. + :param address: An AF_UNIX path or address. + :param sock_fd: Optionally, an existing socket file descriptor. + One of address or sock_fd must be specified. + :param file: Optionally, a filename to log to. + :param drain: Optionally, drains the socket and places the bytes + into an in memory buffer for later processing. """ - def __init__(self, address: str, file: Optional[str] = None, + def __init__(self, + address: Optional[str] = None, + sock_fd: Optional[int] = None, + file: Optional[str] = None, drain: bool = False): + if address is None and sock_fd is None: + raise ValueError("one of 'address' or 'sock_fd' must be specified") + if address is not None and sock_fd is not None: + raise ValueError("can't specify both 'address' and 'sock_fd'") + self._recv_timeout_sec = 300.0 self._sleep_time = 0.5 self._buffer: Deque[int] = deque() - socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM) - self.connect(address) + if address is not None: + socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM) + self.connect(address) + else: + assert sock_fd is not None + socket.socket.__init__(self, fileno=sock_fd) self._logfile = None if file: # pylint: disable=consider-using-with
Signature · 1 line
2.41.0
PATCH v3 2/6 python/machine: close sock_pair in cleanup path
In reply to John SnowIf everything has gone smoothly, we'll already have closed the socket we gave to the child during post_launch. The other half of the pair that we gave to the QMP connection should, likewise, be definitively closed by now.
However, in the cleanup path, it's possible we've created the socketpair but flubbed the launch and need to clean up resources. These resources *would* be handled by the garbage collector, but that can happen at unpredictable times. Nicer to just clean them up synchronously on the exit path, here.
--- python/qemu/machine/machine.py | 5 +++++ 1 file changed, 5 insertions(+)
python/qemu/machine/machine.py +5 -0
@@ -396,6 +396,11 @@ def _post_shutdown(self) -> None: finally: assert self._qmp_connection is None + if self._sock_pair: + self._sock_pair[0].close() + self._sock_pair[1].close() + self._sock_pair = None + self._close_qemu_log_file() self._load_io_log()
Signature · 1 line
2.41.0
v3 cover python/machine: use socketpair() for console socket
Like we did for the QMP socket, use socketpair() for the console socket so that hopefully there isn't a race condition during early boot where data might get dropped on the floor.
May or may not help with various race conditions where early console output is not showing up in the logs and/or potentially being missed by wait_for_console_pattern.
V3:
- Rebased.
V2:
- Fixed some Socket ownership/garbage collection problems
- Fixed callers of now-dropped VM arguments/properties
- added a dedicated sock_fd arg to ConsoleSocket()
- now using socketpair() for qtest console, too.
- dropped sock_dir arg from *all* machine.py classes
- Tested quite a bit more thoroughly ...
CI: https://gitlab.com/jsnow/qemu/-/pipelines/1019123030
John Snow (6):
python/machine: move socket setup out of _base_args property python/machine: close sock_pair in cleanup path python/console_socket: accept existing FD in initializer python/machine: use socketpair() for console connections python/machine: use socketpair() for qtest connection python/machine: remove unused sock_dir argument
python/qemu/machine/console_socket.py | 29 ++++++++--- python/qemu/machine/machine.py | 58 +++++++++++++--------- python/qemu/machine/qtest.py | 54 +++++++++++++++----- tests/avocado/acpi-bits.py | 5 +- tests/avocado/avocado_qemu/__init__.py | 2 +- tests/avocado/machine_aspeed.py | 5 +- tests/qemu-iotests/iotests.py | 2 +- tests/qemu-iotests/tests/copy-before-write | 3 +- 8 files changed, 104 insertions(+), 54 deletions(-)
Signature · 1 line
2.41.0
PATCH v3 1/6 python/machine: move socket setup out of _base_args property
This property isn't meant to do much else besides return a list of strings, so move this setup back out into _pre_launch().
--- python/qemu/machine/machine.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
python/qemu/machine/machine.py +2 -3
@@ -301,9 +301,7 @@ def _base_args(self) -> List[str]: if self._qmp_set: if self._sock_pair: - fd = self._sock_pair[0].fileno() - os.set_inheritable(fd, True) - moncdev = f"socket,id=mon,fd={fd}" + moncdev = f"socket,id=mon,fd={self._sock_pair[0].fileno()}" elif isinstance(self._monitor_address, tuple): moncdev = "socket,id=mon,host={},port={}".format( *self._monitor_address
@@ -340,6 +338,7 @@ def _pre_launch(self) -> None: if self._qmp_set: if self._monitor_address is None: self._sock_pair = socket.socketpair() + os.set_inheritable(self._sock_pair[0].fileno(), True) sock = self._sock_pair[1] if isinstance(self._monitor_address, str): self._remove_files.append(self._monitor_address)
Signature · 1 line
2.41.0
PATCH v3 4/6 python/machine: use socketpair() for console connections
In reply to John SnowCreate a socketpair for the console output. This should help eliminate race conditions around console text early in the boot process that might otherwise have been dropped on the floor before being able to connect to QEMU under "server,nowait".
--- python/qemu/machine/machine.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-)
python/qemu/machine/machine.py +27 -3
@@ -159,6 +159,8 @@ def __init__(self, self._name = name or f"{id(self):x}" self._sock_pair: Optional[Tuple[socket.socket, socket.socket]] = None + self._cons_sock_pair: Optional[ + Tuple[socket.socket, socket.socket]] = None self._temp_dir: Optional[str] = None self._base_temp_dir = base_temp_dir self._sock_dir = sock_dir
@@ -316,8 +318,9 @@ def _base_args(self) -> List[str]: for _ in range(self._console_index): args.extend(['-serial', 'null']) if self._console_set: - chardev = ('socket,id=console,path=%s,server=on,wait=off' % - self._console_address) + assert self._cons_sock_pair is not None + fd = self._cons_sock_pair[0].fileno() + chardev = f"socket,id=console,fd={fd}" args.extend(['-chardev', chardev]) if self._console_device_type is None: args.extend(['-serial', 'chardev:console'])
@@ -352,6 +355,10 @@ def _pre_launch(self) -> None: nickname=self._name ) + if self._console_set: + self._cons_sock_pair = socket.socketpair() + os.set_inheritable(self._cons_sock_pair[0].fileno(), True) + # NOTE: Make sure any opened resources are *definitely* freed in # _post_shutdown()! # pylint: disable=consider-using-with
@@ -369,6 +376,9 @@ def _pre_launch(self) -> None: def _post_launch(self) -> None: if self._sock_pair: self._sock_pair[0].close() + if self._cons_sock_pair: + self._cons_sock_pair[0].close() + if self._qmp_connection: if self._sock_pair: self._qmp.connect()
@@ -524,6 +534,11 @@ def _early_cleanup(self) -> None: self._console_socket.close() self._console_socket = None + if self._cons_sock_pair: + self._cons_sock_pair[0].close() + self._cons_sock_pair[1].close() + self._cons_sock_pair = None + def _hard_shutdown(self) -> None: """ Perform early cleanup, kill the VM, and wait for it to terminate.
@@ -885,10 +900,19 @@ def console_socket(self) -> socket.socket: """ if self._console_socket is None: LOG.debug("Opening console socket") + if not self._console_set: + raise QEMUMachineError( + "Attempt to access console socket with no connection") + assert self._cons_sock_pair is not None + # os.dup() is used here for sock_fd because otherwise we'd + # have two rich python socket objects that would each try to + # close the same underlying fd when either one gets garbage + # collected. self._console_socket = console_socket.ConsoleSocket( - self._console_address, + sock_fd=os.dup(self._cons_sock_pair[1].fileno()), file=self._console_log_path, drain=self._drain_console) + self._cons_sock_pair[1].close() return self._console_socket @property
Signature · 1 line
2.41.0
Quoted 10 lines · John Snow
Like we did for the QMP socket, use
socketpair()for the console socket so that hopefully there isn't a race condition during early boot where data might get dropped on the floor.May or may not help with various race conditions where early console output is not showing up in the logs and/or potentially being missed by
wait_for_console_pattern.V3:
- Rebased.
V3 has R-B on every single patch already. Should this just have been a PULL instead ?
Quoted 19 lines
V2:
- Fixed some Socket ownership/garbage collection problems
- Fixed callers of now-dropped VM arguments/properties
- added a dedicatedsock_fdarg toConsoleSocket()
- now usingsocketpair()for qtest console, too.
- droppedsock_dirarg from *all*machine.pyclasses
- Tested quite a bit more thoroughly ...CI: https://gitlab.com/jsnow/qemu/-/pipelines/1019123030
John Snow (6):
python/machine: move socket setup out of_base_argsproperty python/machine: closesock_pairin cleanup path python/console_socket: accept existing FD in initializer python/machine: usesocketpair()for console connections python/machine: usesocketpair()for qtest connection python/machine: remove unusedsock_dirargument
Quoted 14 lines
python/qemu/machine/console_socket.py | 29 ++++++++--- python/qemu/machine/machine.py | 58 +++++++++++++--------- python/qemu/machine/qtest.py | 54 +++++++++++++++----- tests/avocado/acpi-bits.py | 5 +- tests/avocado/avocado_qemu/__init__.py | 2 +- tests/avocado/machine_aspeed.py | 5 +- tests/qemu-iotests/iotests.py | 2 +- tests/qemu-iotests/tests/copy-before-write | 3 +- 8 files changed, 104 insertions(+), 54 deletions(-)Signature · 1 line
2.41.0
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · John Snow
Like we did for the QMP socket, use
socketpair()for the console socket so that hopefully there isn't a race condition during early boot where data might get dropped on the floor.May or may not help with various race conditions where early console output is not showing up in the logs and/or potentially being missed by
wait_for_console_pattern.
Quoted 6 lines
Quoted 2 lines
V3:
- Rebased.V3 has R-B on every single patch already. Should this just have been a PULL instead ?
I guess just erring on the side of caution with some fresh patches for patchew since it had been a while since V2 and I did have to rebase this.
I'll send the PR soon if there's no objections.
Quoted 45 lines
Quoted 29 lines
V2:
- Fixed some Socket ownership/garbage collection problems
- Fixed callers of now-dropped VM arguments/properties
- added a dedicatedsock_fdarg toConsoleSocket()
- now usingsocketpair()for qtest console, too.
- droppedsock_dirarg from *all*machine.pyclasses
- Tested quite a bit more thoroughly ...CI: https://gitlab.com/jsnow/qemu/-/pipelines/1019123030
John Snow (6):
python/machine: move socket setup out of_base_argsproperty python/machine: closesock_pairin cleanup path python/console_socket: accept existing FD in initializer python/machine: usesocketpair()for console connections python/machine: usesocketpair()for qtest connection python/machine: remove unusedsock_dirargumentpython/qemu/machine/console_socket.py | 29 ++++++++--- python/qemu/machine/machine.py | 58 +++++++++++++--------- python/qemu/machine/qtest.py | 54 +++++++++++++++----- tests/avocado/acpi-bits.py | 5 +- tests/avocado/avocado_qemu/__init__.py | 2 +- tests/avocado/machine_aspeed.py | 5 +- tests/qemu-iotests/iotests.py | 2 +- tests/qemu-iotests/tests/copy-before-write | 3 +- 8 files changed, 104 insertions(+), 54 deletions(-)Quoted 4 lines
--
2.41.0With regards,
Daniel
--
|: https://berrange.com -o-
https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o-
https://fstop138.berrange.com :|
|: https://entangle-photo.org -o-
https://www.instagram.com/dberrange :|