blob: 20b6ec795e38daff24718c0976881863feba58fe [file] [log] [blame]
Luiz Capitulinocedebda2009-11-26 22:59:09 -02001# QEMU Monitor Protocol Python class
Luiz Capitulino22f39462013-09-10 16:39:23 -04002#
Luiz Capitulino1d00a072010-10-27 17:43:34 -02003# Copyright (C) 2009, 2010 Red Hat Inc.
Luiz Capitulinocedebda2009-11-26 22:59:09 -02004#
5# Authors:
6# Luiz Capitulino <lcapitulino@redhat.com>
7#
8# This work is licensed under the terms of the GNU GPL, version 2. See
9# the COPYING file in the top-level directory.
10
Luiz Capitulino1d00a072010-10-27 17:43:34 -020011import json
12import errno
13import socket
Luiz Capitulinocedebda2009-11-26 22:59:09 -020014
15class QMPError(Exception):
16 pass
17
18class QMPConnectError(QMPError):
19 pass
20
Luiz Capitulino1d00a072010-10-27 17:43:34 -020021class QMPCapabilitiesError(QMPError):
22 pass
23
Luiz Capitulinocedebda2009-11-26 22:59:09 -020024class QEMUMonitorProtocol:
Stefan Hajnoczi37628f12011-05-25 19:48:01 +010025 def __init__(self, address, server=False):
Luiz Capitulino1d00a072010-10-27 17:43:34 -020026 """
27 Create a QEMUMonitorProtocol class.
Luiz Capitulinocedebda2009-11-26 22:59:09 -020028
Luiz Capitulino1d00a072010-10-27 17:43:34 -020029 @param address: QEMU address, can be either a unix socket path (string)
30 or a tuple in the form ( address, port ) for a TCP
31 connection
Stefan Hajnoczi37628f12011-05-25 19:48:01 +010032 @param server: server mode listens on the socket (bool)
33 @raise socket.error on socket connection errors
34 @note No connection is established, this is done by the connect() or
35 accept() methods
Luiz Capitulino1d00a072010-10-27 17:43:34 -020036 """
37 self.__events = []
38 self.__address = address
39 self.__sock = self.__get_sock()
Stefan Hajnoczi37628f12011-05-25 19:48:01 +010040 if server:
41 self.__sock.bind(self.__address)
42 self.__sock.listen(1)
Luiz Capitulinocedebda2009-11-26 22:59:09 -020043
Luiz Capitulino1d00a072010-10-27 17:43:34 -020044 def __get_sock(self):
45 if isinstance(self.__address, tuple):
46 family = socket.AF_INET
Luiz Capitulinocedebda2009-11-26 22:59:09 -020047 else:
Luiz Capitulino1d00a072010-10-27 17:43:34 -020048 family = socket.AF_UNIX
49 return socket.socket(family, socket.SOCK_STREAM)
Luiz Capitulinocedebda2009-11-26 22:59:09 -020050
Stefan Hajnoczi37628f12011-05-25 19:48:01 +010051 def __negotiate_capabilities(self):
Stefan Hajnoczi37628f12011-05-25 19:48:01 +010052 greeting = self.__json_read()
53 if greeting is None or not greeting.has_key('QMP'):
54 raise QMPConnectError
55 # Greeting seems ok, negotiate capabilities
56 resp = self.cmd('qmp_capabilities')
57 if "return" in resp:
58 return greeting
59 raise QMPCapabilitiesError
60
Stefan Hajnoczi91b8edd2011-05-25 19:48:00 +010061 def __json_read(self, only_event=False):
Luiz Capitulino1d00a072010-10-27 17:43:34 -020062 while True:
63 data = self.__sockfile.readline()
64 if not data:
65 return
66 resp = json.loads(data)
67 if 'event' in resp:
68 self.__events.append(resp)
Stefan Hajnoczi91b8edd2011-05-25 19:48:00 +010069 if not only_event:
70 continue
Luiz Capitulino1d00a072010-10-27 17:43:34 -020071 return resp
Luiz Capitulinocedebda2009-11-26 22:59:09 -020072
Luiz Capitulino1d00a072010-10-27 17:43:34 -020073 error = socket.error
74
Ryota Ozakie9d17b62012-09-14 21:44:20 +090075 def connect(self, negotiate=True):
Luiz Capitulino1d00a072010-10-27 17:43:34 -020076 """
77 Connect to the QMP Monitor and perform capabilities negotiation.
78
79 @return QMP greeting dict
80 @raise socket.error on socket connection errors
81 @raise QMPConnectError if the greeting is not received
82 @raise QMPCapabilitiesError if fails to negotiate capabilities
83 """
84 self.__sock.connect(self.__address)
Ryota Ozakie9d17b62012-09-14 21:44:20 +090085 self.__sockfile = self.__sock.makefile()
86 if negotiate:
87 return self.__negotiate_capabilities()
Stefan Hajnoczi37628f12011-05-25 19:48:01 +010088
89 def accept(self):
90 """
91 Await connection from QMP Monitor and perform capabilities negotiation.
92
93 @return QMP greeting dict
94 @raise socket.error on socket connection errors
95 @raise QMPConnectError if the greeting is not received
96 @raise QMPCapabilitiesError if fails to negotiate capabilities
97 """
98 self.__sock, _ = self.__sock.accept()
Jeff Codyb3d03802012-10-15 16:58:02 -040099 self.__sockfile = self.__sock.makefile()
Stefan Hajnoczi37628f12011-05-25 19:48:01 +0100100 return self.__negotiate_capabilities()
Luiz Capitulino1d00a072010-10-27 17:43:34 -0200101
102 def cmd_obj(self, qmp_cmd):
103 """
104 Send a QMP command to the QMP Monitor.
105
106 @param qmp_cmd: QMP command to be sent as a Python dict
107 @return QMP response as a Python dict or None if the connection has
108 been closed
109 """
110 try:
111 self.__sock.sendall(json.dumps(qmp_cmd))
112 except socket.error, err:
113 if err[0] == errno.EPIPE:
114 return
115 raise socket.error(err)
116 return self.__json_read()
117
118 def cmd(self, name, args=None, id=None):
119 """
120 Build a QMP command and send it to the QMP Monitor.
121
122 @param name: command name (string)
123 @param args: command arguments (dict)
124 @param id: command id (dict, list, string or int)
125 """
126 qmp_cmd = { 'execute': name }
127 if args:
128 qmp_cmd['arguments'] = args
129 if id:
130 qmp_cmd['id'] = id
131 return self.cmd_obj(qmp_cmd)
132
Anthony Liguori9f68f7f2012-02-20 16:28:29 -0600133 def command(self, cmd, **kwds):
134 ret = self.cmd(cmd, kwds)
135 if ret.has_key('error'):
136 raise Exception(ret['error']['desc'])
137 return ret['return']
138
Paolo Bonzini9eb80ea2012-10-18 16:49:29 +0200139 def pull_event(self, wait=False):
140 """
141 Get and delete the first available QMP event.
142
143 @param wait: block until an event is available (bool)
144 """
145 self.__sock.setblocking(0)
146 try:
147 self.__json_read()
148 except socket.error, err:
149 if err[0] == errno.EAGAIN:
150 # No data available
151 pass
152 self.__sock.setblocking(1)
153 if not self.__events and wait:
154 self.__json_read(only_event=True)
155 event = self.__events[0]
156 del self.__events[0]
157 return event
158
Stefan Hajnoczi91b8edd2011-05-25 19:48:00 +0100159 def get_events(self, wait=False):
Luiz Capitulino1d00a072010-10-27 17:43:34 -0200160 """
161 Get a list of available QMP events.
Stefan Hajnoczi91b8edd2011-05-25 19:48:00 +0100162
163 @param wait: block until an event is available (bool)
Luiz Capitulino1d00a072010-10-27 17:43:34 -0200164 """
165 self.__sock.setblocking(0)
166 try:
167 self.__json_read()
168 except socket.error, err:
169 if err[0] == errno.EAGAIN:
170 # No data available
171 pass
172 self.__sock.setblocking(1)
Stefan Hajnoczi91b8edd2011-05-25 19:48:00 +0100173 if not self.__events and wait:
Fam Zheng48645122014-02-24 13:45:01 +0800174 ret = self.__json_read(only_event=True)
175 if ret == None:
176 # We are in blocking mode, if don't get anything, something
177 # went wrong
178 raise QMPConnectError("Error while reading from socket")
179
Luiz Capitulino1d00a072010-10-27 17:43:34 -0200180 return self.__events
181
182 def clear_events(self):
183 """
184 Clear current list of pending events.
185 """
186 self.__events = []
187
188 def close(self):
189 self.__sock.close()
190 self.__sockfile.close()
Ryota Ozakie37b3502012-09-14 21:44:21 +0900191
192 timeout = socket.timeout
193
194 def settimeout(self, timeout):
195 self.__sock.settimeout(timeout)
Wenchao Xia30b005d2013-09-06 11:24:33 +0800196
197 def get_sock_fd(self):
198 return self.__sock.fileno()
199
200 def is_scm_available(self):
201 return self.__sock.family == socket.AF_UNIX