1 # Copyright (c) 2012 Nicira Networks
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
28 Message = ovs.jsonrpc.Message
29 vlog = ovs.vlog.Vlog("unixctl")
31 strtypes = types.StringTypes
34 class _UnixctlCommand(object):
35 def __init__(self, usage, min_args, max_args, callback, aux):
37 self.min_args = min_args
38 self.max_args = max_args
39 self.callback = callback
43 def _unixctl_help(conn, unused_argv, unused_aux):
44 assert isinstance(conn, UnixctlConnection)
45 reply = "The available commands are:\n"
46 command_names = sorted(commands.keys())
47 for name in command_names:
49 usage = commands[name].usage
51 reply += "%-23s %s" % (name, usage)
58 def _unixctl_version(conn, unused_argv, unused_aux):
59 assert isinstance(conn, UnixctlConnection)
60 version = "%s (Open vSwitch) %s%s" % (ovs.util.PROGRAM_NAME,
66 def command_register(name, usage, min_args, max_args, callback, aux):
67 """ Registers a command with the given 'name' to be exposed by the
68 UnixctlServer. 'usage' describes the arguments to the command; it is used
69 only for presentation to the user in "help" output.
71 'callback' is called when the command is received. It is passed a
72 UnixctlConnection object, the list of arguments as unicode strings, and
73 'aux'. Normally 'callback' should reply by calling
74 UnixctlConnection.reply() or UnixctlConnection.reply_error() before it
75 returns, but if the command cannot be handled immediately, then it can
76 defer the reply until later. A given connection can only process a single
77 request at a time, so a reply must be made eventually to avoid blocking
80 assert isinstance(name, strtypes)
81 assert isinstance(usage, strtypes)
82 assert isinstance(min_args, int)
83 assert isinstance(max_args, int)
84 assert isinstance(callback, types.FunctionType)
86 if name not in commands:
87 commands[name] = _UnixctlCommand(usage, min_args, max_args, callback,
91 def socket_name_from_target(target):
92 assert isinstance(target, strtypes)
94 if target.startswith("/"):
97 pidfile_name = "%s/%s.pid" % (ovs.dirs.RUNDIR, target)
98 pid = ovs.daemon.read_pidfile(pidfile_name)
100 return -pid, "cannot read pidfile \"%s\"" % pidfile_name
102 return 0, "%s/%s.%d.ctl" % (ovs.dirs.RUNDIR, target, pid)
105 class UnixctlConnection(object):
106 def __init__(self, rpc):
107 assert isinstance(rpc, ovs.jsonrpc.Connection)
109 self._request_id = None
113 error = self._rpc.get_status()
114 if error or self._rpc.get_backlog():
118 if error or self._request_id:
121 error, msg = self._rpc.recv()
123 if msg.type == Message.T_REQUEST:
124 self._process_command(msg)
127 vlog.warn("%s: received unexpected %s message"
129 Message.type_to_string(msg.type)))
133 error = self._rpc.get_status()
137 def reply(self, body):
138 self._reply_impl(True, body)
140 def reply_error(self, body):
141 self._reply_impl(False, body)
143 # Called only by unixctl classes.
146 self._request_id = None
148 def _wait(self, poller):
149 self._rpc.wait(poller)
150 if not self._rpc.get_backlog():
151 self._rpc.recv_wait(poller)
153 def _reply_impl(self, success, body):
154 assert isinstance(success, bool)
155 assert body is None or isinstance(body, strtypes)
157 assert self._request_id is not None
162 if body and not body.endswith("\n"):
166 reply = Message.create_reply(body, self._request_id)
168 reply = Message.create_error(body, self._request_id)
170 self._rpc.send(reply)
171 self._request_id = None
173 def _process_command(self, request):
174 assert isinstance(request, ovs.jsonrpc.Message)
175 assert request.type == ovs.jsonrpc.Message.T_REQUEST
177 self._request_id = request.id
180 params = request.params
181 method = request.method
182 command = commands.get(method)
184 error = '"%s" is not a valid command' % method
185 elif len(params) < command.min_args:
186 error = '"%s" command requires at least %d arguments' \
187 % (method, command.min_args)
188 elif len(params) > command.max_args:
189 error = '"%s" command takes at most %d arguments' \
190 % (method, command.max_args)
193 if not isinstance(param, strtypes):
194 error = '"%s" command has non-string argument' % method
198 unicode_params = [unicode(p) for p in params]
199 command.callback(self, unicode_params, command.aux)
202 self.reply_error(error)
205 class UnixctlServer(object):
206 def __init__(self, listener):
207 assert isinstance(listener, ovs.stream.PassiveStream)
208 self._listener = listener
213 error, stream = self._listener.accept()
215 rpc = ovs.jsonrpc.Connection(stream)
216 self._conns.append(UnixctlConnection(rpc))
217 elif error == errno.EAGAIN:
221 vlog.warn("%s: accept failed: %s" % (self._listener.name,
224 for conn in copy.copy(self._conns):
226 if error and error != errno.EAGAIN:
228 self._conns.remove(conn)
230 def wait(self, poller):
231 self._listener.wait(poller)
232 for conn in self._conns:
236 for conn in self._conns:
240 self._listener.close()
241 self._listener = None
245 assert path is None or isinstance(path, strtypes)
248 path = "punix:%s" % ovs.util.abs_file_name(ovs.dirs.RUNDIR, path)
250 path = "punix:%s/%s.%d.ctl" % (ovs.dirs.RUNDIR,
251 ovs.util.PROGRAM_NAME, os.getpid())
253 error, listener = ovs.stream.PassiveStream.open(path)
255 ovs.util.ovs_error(error, "could not initialize control socket %s"
259 command_register("help", "", 0, 0, _unixctl_help, None)
260 command_register("version", "", 0, 0, _unixctl_version, None)
262 return 0, UnixctlServer(listener)
265 class UnixctlClient(object):
266 def __init__(self, conn):
267 assert isinstance(conn, ovs.jsonrpc.Connection)
270 def transact(self, command, argv):
271 assert isinstance(command, strtypes)
272 assert isinstance(argv, list)
274 assert isinstance(arg, strtypes)
276 request = Message.create_request(command, argv)
277 error, reply = self._conn.transact_block(request)
280 vlog.warn("error communicating with %s: %s"
281 % (self._conn.name, os.strerror(error)))
282 return error, None, None
284 if reply.error is not None:
285 return 0, str(reply.error), None
287 assert reply.result is not None
288 return 0, None, str(reply.result)
296 assert isinstance(path, str)
298 unix = "unix:%s" % ovs.util.abs_file_name(ovs.dirs.RUNDIR, path)
299 error, stream = ovs.stream.Stream.open_block(
300 ovs.stream.Stream.open(unix))
303 vlog.warn("failed to connect to %s" % path)
306 return 0, UnixctlClient(ovs.jsonrpc.Connection(stream))