]>
git.ipfire.org Git - thirdparty/qemu.git/blob - python/qemu/qmp.py
1 """ QEMU Monitor Protocol Python class """
2 # Copyright (C) 2009, 2010 Red Hat Inc.
5 # Luiz Capitulino <lcapitulino@redhat.com>
7 # This work is licensed under the terms of the GNU GPL, version 2. See
8 # the COPYING file in the top-level directory.
16 class QMPError(Exception):
22 class QMPConnectError(QMPError
):
24 QMP connection exception
28 class QMPCapabilitiesError(QMPError
):
30 QMP negotiate capabilities exception
34 class QMPTimeoutError(QMPError
):
40 class QEMUMonitorProtocol
:
42 Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then
43 allow to handle commands and events.
46 #: Logger object for debugging messages
47 logger
= logging
.getLogger('QMP')
49 def __init__(self
, address
, server
=False, nickname
=None):
51 Create a QEMUMonitorProtocol class.
53 @param address: QEMU address, can be either a unix socket path (string)
54 or a tuple in the form ( address, port ) for a TCP
56 @param server: server mode listens on the socket (bool)
57 @raise OSError on socket connection errors
58 @note No connection is established, this is done by the connect() or
62 self
.__address
= address
63 self
.__sock
= self
.__get
_sock
()
64 self
.__sockfile
= None
65 self
._nickname
= nickname
67 self
.logger
= logging
.getLogger('QMP').getChild(self
._nickname
)
69 self
.__sock
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
70 self
.__sock
.bind(self
.__address
)
74 if isinstance(self
.__address
, tuple):
75 family
= socket
.AF_INET
77 family
= socket
.AF_UNIX
78 return socket
.socket(family
, socket
.SOCK_STREAM
)
80 def __negotiate_capabilities(self
):
81 greeting
= self
.__json
_read
()
82 if greeting
is None or "QMP" not in greeting
:
84 # Greeting seems ok, negotiate capabilities
85 resp
= self
.cmd('qmp_capabilities')
86 if resp
and "return" in resp
:
88 raise QMPCapabilitiesError
90 def __json_read(self
, only_event
=False):
92 data
= self
.__sockfile
.readline()
95 resp
= json
.loads(data
)
97 self
.logger
.debug("<<< %s", resp
)
98 self
.__events
.append(resp
)
103 def __get_events(self
, wait
=False):
105 Check for new events in the stream and cache them in __events.
107 @param wait (bool): block until an event is available.
108 @param wait (float): If wait is a float, treat it as a timeout value.
110 @raise QMPTimeoutError: If a timeout float is provided and the timeout
112 @raise QMPConnectError: If wait is True but no events could be
113 retrieved or if some other error occurred.
116 # Check for new events regardless and pull them into the cache:
117 self
.__sock
.setblocking(0)
120 except OSError as err
:
121 if err
.errno
== errno
.EAGAIN
:
124 self
.__sock
.setblocking(1)
126 # Wait for new events, if needed.
127 # if wait is 0.0, this means "no wait" and is also implicitly false.
128 if not self
.__events
and wait
:
129 if isinstance(wait
, float):
130 self
.__sock
.settimeout(wait
)
132 ret
= self
.__json
_read
(only_event
=True)
133 except socket
.timeout
:
134 raise QMPTimeoutError("Timeout waiting for event")
136 raise QMPConnectError("Error while reading from socket")
138 raise QMPConnectError("Error while reading from socket")
139 self
.__sock
.settimeout(None)
142 # Implement context manager enter function.
145 def __exit__(self
, exc_type
, exc_value
, exc_traceback
):
146 # Implement context manager exit function.
150 def connect(self
, negotiate
=True):
152 Connect to the QMP Monitor and perform capabilities negotiation.
154 @return QMP greeting dict, or None if negotiate is false
155 @raise OSError on socket connection errors
156 @raise QMPConnectError if the greeting is not received
157 @raise QMPCapabilitiesError if fails to negotiate capabilities
159 self
.__sock
.connect(self
.__address
)
160 self
.__sockfile
= self
.__sock
.makefile()
162 return self
.__negotiate
_capabilities
()
165 def accept(self
, timeout
=15.0):
167 Await connection from QMP Monitor and perform capabilities negotiation.
169 @param timeout: timeout in seconds (nonnegative float number, or
170 None). The value passed will set the behavior of the
171 underneath QMP socket as described in [1]. Default value
173 @return QMP greeting dict
174 @raise OSError on socket connection errors
175 @raise QMPConnectError if the greeting is not received
176 @raise QMPCapabilitiesError if fails to negotiate capabilities
179 https://docs.python.org/3/library/socket.html#socket.socket.settimeout
181 self
.__sock
.settimeout(timeout
)
182 self
.__sock
, _
= self
.__sock
.accept()
183 self
.__sockfile
= self
.__sock
.makefile()
184 return self
.__negotiate
_capabilities
()
186 def cmd_obj(self
, qmp_cmd
):
188 Send a QMP command to the QMP Monitor.
190 @param qmp_cmd: QMP command to be sent as a Python dict
191 @return QMP response as a Python dict or None if the connection has
194 self
.logger
.debug(">>> %s", qmp_cmd
)
196 self
.__sock
.sendall(json
.dumps(qmp_cmd
).encode('utf-8'))
197 except OSError as err
:
198 if err
.errno
== errno
.EPIPE
:
201 resp
= self
.__json
_read
()
202 self
.logger
.debug("<<< %s", resp
)
205 def cmd(self
, name
, args
=None, cmd_id
=None):
207 Build a QMP command and send it to the QMP Monitor.
209 @param name: command name (string)
210 @param args: command arguments (dict)
211 @param cmd_id: command id (dict, list, string or int)
213 qmp_cmd
= {'execute': name
}
215 qmp_cmd
['arguments'] = args
217 qmp_cmd
['id'] = cmd_id
218 return self
.cmd_obj(qmp_cmd
)
220 def command(self
, cmd
, **kwds
):
222 Build and send a QMP command to the monitor, report errors if any
224 ret
= self
.cmd(cmd
, kwds
)
226 raise Exception(ret
['error']['desc'])
229 def pull_event(self
, wait
=False):
231 Pulls a single event.
233 @param wait (bool): block until an event is available.
234 @param wait (float): If wait is a float, treat it as a timeout value.
236 @raise QMPTimeoutError: If a timeout float is provided and the timeout
238 @raise QMPConnectError: If wait is True but no events could be
239 retrieved or if some other error occurred.
241 @return The first available QMP event, or None.
243 self
.__get
_events
(wait
)
246 return self
.__events
.pop(0)
249 def get_events(self
, wait
=False):
251 Get a list of available QMP events.
253 @param wait (bool): block until an event is available.
254 @param wait (float): If wait is a float, treat it as a timeout value.
256 @raise QMPTimeoutError: If a timeout float is provided and the timeout
258 @raise QMPConnectError: If wait is True but no events could be
259 retrieved or if some other error occurred.
261 @return The list of available QMP events.
263 self
.__get
_events
(wait
)
266 def clear_events(self
):
268 Clear current list of pending events.
274 Close the socket and socket file.
279 self
.__sockfile
.close()
281 def settimeout(self
, timeout
):
283 Set the socket timeout.
285 @param timeout (float): timeout in seconds, or None.
286 @note This is a wrap around socket.settimeout
288 self
.__sock
.settimeout(timeout
)
290 def get_sock_fd(self
):
292 Get the socket file descriptor.
294 @return The file descriptor number.
296 return self
.__sock
.fileno()
298 def is_scm_available(self
):
300 Check if the socket allows for SCM_RIGHTS.
302 @return True if SCM_RIGHTS is available, otherwise False.
304 return self
.__sock
.family
== socket
.AF_UNIX