1beb6b57bSJohn Snow""" 2beb6b57bSJohn SnowQEMU qtest library 3beb6b57bSJohn Snow 4beb6b57bSJohn Snowqtest offers the QEMUQtestProtocol and QEMUQTestMachine classes, which 5beb6b57bSJohn Snowoffer a connection to QEMU's qtest protocol socket, and a qtest-enabled 6beb6b57bSJohn Snowsubclass of QEMUMachine, respectively. 7beb6b57bSJohn Snow""" 8beb6b57bSJohn Snow 9beb6b57bSJohn Snow# Copyright (C) 2015 Red Hat Inc. 10beb6b57bSJohn Snow# 11beb6b57bSJohn Snow# Authors: 12beb6b57bSJohn Snow# Fam Zheng <famz@redhat.com> 13beb6b57bSJohn Snow# 14beb6b57bSJohn Snow# This work is licensed under the terms of the GNU GPL, version 2. See 15beb6b57bSJohn Snow# the COPYING file in the top-level directory. 16beb6b57bSJohn Snow# 17beb6b57bSJohn Snow# Based on qmp.py. 18beb6b57bSJohn Snow# 19beb6b57bSJohn Snow 20beb6b57bSJohn Snowimport os 21beb6b57bSJohn Snowimport socket 22beb6b57bSJohn Snowfrom typing import ( 23beb6b57bSJohn Snow List, 24beb6b57bSJohn Snow Optional, 25beb6b57bSJohn Snow Sequence, 26beb6b57bSJohn Snow TextIO, 27*d3967378SJohn Snow Tuple, 28beb6b57bSJohn Snow) 29beb6b57bSJohn Snow 3037094b6dSJohn Snowfrom qemu.qmp import SocketAddrT 31beb6b57bSJohn Snow 32beb6b57bSJohn Snowfrom .machine import QEMUMachine 33beb6b57bSJohn Snow 34beb6b57bSJohn Snow 35beb6b57bSJohn Snowclass QEMUQtestProtocol: 36beb6b57bSJohn Snow """ 37beb6b57bSJohn Snow QEMUQtestProtocol implements a connection to a qtest socket. 38beb6b57bSJohn Snow 39beb6b57bSJohn Snow :param address: QEMU address, can be either a unix socket path (string) 40beb6b57bSJohn Snow or a tuple in the form ( address, port ) for a TCP 41beb6b57bSJohn Snow connection 42*d3967378SJohn Snow :param sock: An existing socket can be provided as an alternative to 43*d3967378SJohn Snow an address. One of address or sock must be provided. 44*d3967378SJohn Snow :param server: server mode, listens on the socket. Only meaningful 45*d3967378SJohn Snow in conjunction with an address and not an existing 46*d3967378SJohn Snow socket. 47*d3967378SJohn Snow 48beb6b57bSJohn Snow :raise socket.error: on socket connection errors 49beb6b57bSJohn Snow 50beb6b57bSJohn Snow .. note:: 51af76484eSDongdong Zhang No connection is established by __init__(), this is done 52beb6b57bSJohn Snow by the connect() or accept() methods. 53beb6b57bSJohn Snow """ 54*d3967378SJohn Snow def __init__(self, 55*d3967378SJohn Snow address: Optional[SocketAddrT] = None, 56*d3967378SJohn Snow sock: Optional[socket.socket] = None, 57beb6b57bSJohn Snow server: bool = False): 58*d3967378SJohn Snow if address is None and sock is None: 59*d3967378SJohn Snow raise ValueError("Either 'address' or 'sock' must be specified") 60*d3967378SJohn Snow if address is not None and sock is not None: 61*d3967378SJohn Snow raise ValueError( 62*d3967378SJohn Snow "Either 'address' or 'sock' must be specified, but not both") 63*d3967378SJohn Snow if sock is not None and server: 64*d3967378SJohn Snow raise ValueError("server=True is meaningless when passing socket") 65*d3967378SJohn Snow 66beb6b57bSJohn Snow self._address = address 67*d3967378SJohn Snow self._sock = sock or self._get_sock() 68beb6b57bSJohn Snow self._sockfile: Optional[TextIO] = None 69*d3967378SJohn Snow 70beb6b57bSJohn Snow if server: 71*d3967378SJohn Snow assert self._address is not None 72beb6b57bSJohn Snow self._sock.bind(self._address) 73beb6b57bSJohn Snow self._sock.listen(1) 74beb6b57bSJohn Snow 75beb6b57bSJohn Snow def _get_sock(self) -> socket.socket: 76*d3967378SJohn Snow assert self._address is not None 77beb6b57bSJohn Snow if isinstance(self._address, tuple): 78beb6b57bSJohn Snow family = socket.AF_INET 79beb6b57bSJohn Snow else: 80beb6b57bSJohn Snow family = socket.AF_UNIX 81beb6b57bSJohn Snow return socket.socket(family, socket.SOCK_STREAM) 82beb6b57bSJohn Snow 83beb6b57bSJohn Snow def connect(self) -> None: 84beb6b57bSJohn Snow """ 85beb6b57bSJohn Snow Connect to the qtest socket. 86beb6b57bSJohn Snow 87beb6b57bSJohn Snow @raise socket.error on socket connection errors 88beb6b57bSJohn Snow """ 89*d3967378SJohn Snow if self._address is not None: 90beb6b57bSJohn Snow self._sock.connect(self._address) 91beb6b57bSJohn Snow self._sockfile = self._sock.makefile(mode='r') 92beb6b57bSJohn Snow 93beb6b57bSJohn Snow def accept(self) -> None: 94beb6b57bSJohn Snow """ 95beb6b57bSJohn Snow Await connection from QEMU. 96beb6b57bSJohn Snow 97beb6b57bSJohn Snow @raise socket.error on socket connection errors 98beb6b57bSJohn Snow """ 99beb6b57bSJohn Snow self._sock, _ = self._sock.accept() 100beb6b57bSJohn Snow self._sockfile = self._sock.makefile(mode='r') 101beb6b57bSJohn Snow 102beb6b57bSJohn Snow def cmd(self, qtest_cmd: str) -> str: 103beb6b57bSJohn Snow """ 104beb6b57bSJohn Snow Send a qtest command on the wire. 105beb6b57bSJohn Snow 106beb6b57bSJohn Snow @param qtest_cmd: qtest command text to be sent 107beb6b57bSJohn Snow """ 108beb6b57bSJohn Snow assert self._sockfile is not None 109beb6b57bSJohn Snow self._sock.sendall((qtest_cmd + "\n").encode('utf-8')) 110beb6b57bSJohn Snow resp = self._sockfile.readline() 111beb6b57bSJohn Snow return resp 112beb6b57bSJohn Snow 113beb6b57bSJohn Snow def close(self) -> None: 114beb6b57bSJohn Snow """ 115beb6b57bSJohn Snow Close this socket. 116beb6b57bSJohn Snow """ 117beb6b57bSJohn Snow self._sock.close() 118beb6b57bSJohn Snow if self._sockfile: 119beb6b57bSJohn Snow self._sockfile.close() 120beb6b57bSJohn Snow self._sockfile = None 121beb6b57bSJohn Snow 122beb6b57bSJohn Snow def settimeout(self, timeout: Optional[float]) -> None: 123beb6b57bSJohn Snow """Set a timeout, in seconds.""" 124beb6b57bSJohn Snow self._sock.settimeout(timeout) 125beb6b57bSJohn Snow 126beb6b57bSJohn Snow 127beb6b57bSJohn Snowclass QEMUQtestMachine(QEMUMachine): 128beb6b57bSJohn Snow """ 129beb6b57bSJohn Snow A QEMU VM, with a qtest socket available. 130beb6b57bSJohn Snow """ 131beb6b57bSJohn Snow 132beb6b57bSJohn Snow def __init__(self, 133beb6b57bSJohn Snow binary: str, 134beb6b57bSJohn Snow args: Sequence[str] = (), 135804f7695SEmanuele Giuseppe Esposito wrapper: Sequence[str] = (), 136beb6b57bSJohn Snow name: Optional[str] = None, 137beb6b57bSJohn Snow base_temp_dir: str = "/var/tmp", 138e2f948a8SEmanuele Giuseppe Esposito sock_dir: Optional[str] = None, 139e2f948a8SEmanuele Giuseppe Esposito qmp_timer: Optional[float] = None): 14082e6517dSJohn Snow # pylint: disable=too-many-arguments 14182e6517dSJohn Snow 142beb6b57bSJohn Snow if name is None: 143beb6b57bSJohn Snow name = "qemu-%d" % os.getpid() 144beb6b57bSJohn Snow if sock_dir is None: 145beb6b57bSJohn Snow sock_dir = base_temp_dir 146804f7695SEmanuele Giuseppe Esposito super().__init__(binary, args, wrapper=wrapper, name=name, 147804f7695SEmanuele Giuseppe Esposito base_temp_dir=base_temp_dir, 148e2f948a8SEmanuele Giuseppe Esposito sock_dir=sock_dir, qmp_timer=qmp_timer) 149beb6b57bSJohn Snow self._qtest: Optional[QEMUQtestProtocol] = None 150*d3967378SJohn Snow self._qtest_sock_pair: Optional[ 151*d3967378SJohn Snow Tuple[socket.socket, socket.socket]] = None 152beb6b57bSJohn Snow 153beb6b57bSJohn Snow @property 154beb6b57bSJohn Snow def _base_args(self) -> List[str]: 155beb6b57bSJohn Snow args = super()._base_args 156*d3967378SJohn Snow assert self._qtest_sock_pair is not None 157*d3967378SJohn Snow fd = self._qtest_sock_pair[0].fileno() 158beb6b57bSJohn Snow args.extend([ 159*d3967378SJohn Snow '-chardev', f"socket,id=qtest,fd={fd}", 160*d3967378SJohn Snow '-qtest', 'chardev:qtest', 161beb6b57bSJohn Snow '-accel', 'qtest' 162beb6b57bSJohn Snow ]) 163beb6b57bSJohn Snow return args 164beb6b57bSJohn Snow 165beb6b57bSJohn Snow def _pre_launch(self) -> None: 166*d3967378SJohn Snow self._qtest_sock_pair = socket.socketpair() 167*d3967378SJohn Snow os.set_inheritable(self._qtest_sock_pair[0].fileno(), True) 168beb6b57bSJohn Snow super()._pre_launch() 169*d3967378SJohn Snow self._qtest = QEMUQtestProtocol(sock=self._qtest_sock_pair[1]) 170beb6b57bSJohn Snow 171beb6b57bSJohn Snow def _post_launch(self) -> None: 172beb6b57bSJohn Snow assert self._qtest is not None 173beb6b57bSJohn Snow super()._post_launch() 174*d3967378SJohn Snow if self._qtest_sock_pair: 175*d3967378SJohn Snow self._qtest_sock_pair[0].close() 176*d3967378SJohn Snow self._qtest.connect() 177beb6b57bSJohn Snow 178beb6b57bSJohn Snow def _post_shutdown(self) -> None: 179*d3967378SJohn Snow if self._qtest_sock_pair: 180*d3967378SJohn Snow self._qtest_sock_pair[0].close() 181*d3967378SJohn Snow self._qtest_sock_pair[1].close() 182*d3967378SJohn Snow self._qtest_sock_pair = None 183beb6b57bSJohn Snow super()._post_shutdown() 184beb6b57bSJohn Snow 185beb6b57bSJohn Snow def qtest(self, cmd: str) -> str: 186beb6b57bSJohn Snow """ 187beb6b57bSJohn Snow Send a qtest command to the guest. 188beb6b57bSJohn Snow 189beb6b57bSJohn Snow :param cmd: qtest command to send 190beb6b57bSJohn Snow :return: qtest server response 191beb6b57bSJohn Snow """ 192beb6b57bSJohn Snow if self._qtest is None: 193beb6b57bSJohn Snow raise RuntimeError("qtest socket not available") 194beb6b57bSJohn Snow return self._qtest.cmd(cmd) 195