2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "dynamic-string.h"
27 #include "poll-loop.h"
30 #include "stream-provider.h"
34 VLOG_DEFINE_THIS_MODULE(unixctl);
36 COVERAGE_DEFINE(unixctl_received);
37 COVERAGE_DEFINE(unixctl_replied);
39 struct unixctl_command {
41 int min_args, max_args;
50 /* Only one request can be in progress at a time. While the request is
51 * being processed, 'request_id' is populated, otherwise it is null. */
52 struct json *request_id; /* ID of the currently active request. */
55 /* Server for control connection. */
56 struct unixctl_server {
57 struct pstream *listener;
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
63 static struct shash commands = SHASH_INITIALIZER(&commands);
66 unixctl_help(struct unixctl_conn *conn, int argc OVS_UNUSED,
67 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
69 struct ds ds = DS_EMPTY_INITIALIZER;
70 const struct shash_node **nodes = shash_sort(&commands);
73 ds_put_cstr(&ds, "The available commands are:\n");
75 for (i = 0; i < shash_count(&commands); i++) {
76 const struct shash_node *node = nodes[i];
77 const struct unixctl_command *command = node->data;
79 ds_put_format(&ds, " %-23s %s\n", node->name, command->usage);
83 unixctl_command_reply(conn, ds_cstr(&ds));
88 unixctl_version(struct unixctl_conn *conn, int argc OVS_UNUSED,
89 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
91 unixctl_command_reply(conn, get_program_version());
94 /* Registers a unixctl command with the given 'name'. 'usage' describes the
95 * arguments to the command; it is used only for presentation to the user in
98 * 'cb' is called when the command is received. It is passed an array
99 * containing the command name and arguments, plus a copy of 'aux'. Normally
100 * 'cb' should reply by calling unixctl_command_reply() or
101 * unixctl_command_reply_error() before it returns, but if the command cannot
102 * be handled immediately then it can defer the reply until later. A given
103 * connection can only process a single request at a time, so a reply must be
104 * made eventually to avoid blocking that connection. */
106 unixctl_command_register(const char *name, const char *usage,
107 int min_args, int max_args,
108 unixctl_cb_func *cb, void *aux)
110 struct unixctl_command *command;
111 struct unixctl_command *lookup = shash_find_data(&commands, name);
113 ovs_assert(!lookup || lookup->cb == cb);
119 command = xmalloc(sizeof *command);
120 command->usage = usage;
121 command->min_args = min_args;
122 command->max_args = max_args;
125 shash_add(&commands, name, command);
129 unixctl_command_reply__(struct unixctl_conn *conn,
130 bool success, const char *body)
132 struct json *body_json;
133 struct jsonrpc_msg *reply;
135 COVERAGE_INC(unixctl_replied);
136 ovs_assert(conn->request_id);
142 if (body[0] && body[strlen(body) - 1] != '\n') {
143 body_json = json_string_create_nocopy(xasprintf("%s\n", body));
145 body_json = json_string_create(body);
149 reply = jsonrpc_create_reply(body_json, conn->request_id);
151 reply = jsonrpc_create_error(body_json, conn->request_id);
154 /* If jsonrpc_send() returns an error, the run loop will take care of the
155 * problem eventually. */
156 jsonrpc_send(conn->rpc, reply);
157 json_destroy(conn->request_id);
158 conn->request_id = NULL;
161 /* Replies to the active unixctl connection 'conn'. 'result' is sent to the
162 * client indicating the command was processed successfully. Only one call to
163 * unixctl_command_reply() or unixctl_command_reply_error() may be made per
166 unixctl_command_reply(struct unixctl_conn *conn, const char *result)
168 unixctl_command_reply__(conn, true, result);
171 /* Replies to the active unixctl connection 'conn'. 'error' is sent to the
172 * client indicating an error occurred processing the command. Only one call to
173 * unixctl_command_reply() or unixctl_command_reply_error() may be made per
176 unixctl_command_reply_error(struct unixctl_conn *conn, const char *error)
178 unixctl_command_reply__(conn, false, error);
181 /* Creates a unixctl server listening on 'path', which for POSIX may be:
183 * - NULL, in which case <rundir>/<program>.<pid>.ctl is used.
185 * - A name that does not start with '/', in which case it is put in
188 * - An absolute path (starting with '/') that gives the exact name of
189 * the Unix domain socket to listen on.
191 * For Windows, a kernel assigned TCP port is used and written in 'path'
194 * - NULL, in which case <rundir>/<program>.ctl is used.
196 * - An absolute path that gives the name of the file.
198 * For both POSIX and Windows, if the path is "none", the function will
199 * return successfully but no socket will actually be created.
201 * A program that (optionally) daemonizes itself should call this function
202 * *after* daemonization, so that the socket name contains the pid of the
203 * daemon instead of the pid of the program that exited. (Otherwise,
204 * "ovs-appctl --target=<program>" will fail.)
206 * Returns 0 if successful, otherwise a positive errno value. If successful,
207 * sets '*serverp' to the new unixctl_server (or to NULL if 'path' was "none"),
208 * otherwise to NULL. */
210 unixctl_server_create(const char *path, struct unixctl_server **serverp)
212 struct unixctl_server *server;
213 struct pstream *listener;
218 if (path && !strcmp(path, "none")) {
225 abs_path = abs_file_name(ovs_rundir(), path);
227 abs_path = strdup(path);
229 punix_path = xasprintf("punix:%s", abs_path);
233 punix_path = xasprintf("punix:%s/%s.%ld.ctl", ovs_rundir(),
234 program_name, (long int) getpid());
236 punix_path = xasprintf("punix:%s/%s.ctl", ovs_rundir(), program_name);
240 error = pstream_open(punix_path, &listener, 0);
242 ovs_error(error, "could not initialize control socket %s", punix_path);
246 unixctl_command_register("help", "", 0, 0, unixctl_help, NULL);
247 unixctl_command_register("version", "", 0, 0, unixctl_version, NULL);
249 server = xmalloc(sizeof *server);
250 server->listener = listener;
251 list_init(&server->conns);
260 process_command(struct unixctl_conn *conn, struct jsonrpc_msg *request)
264 struct unixctl_command *command;
265 struct json_array *params;
267 COVERAGE_INC(unixctl_received);
268 conn->request_id = json_clone(request->id);
270 params = json_array(request->params);
271 command = shash_find_data(&commands, request->method);
273 error = xasprintf("\"%s\" is not a valid command", request->method);
274 } else if (params->n < command->min_args) {
275 error = xasprintf("\"%s\" command requires at least %d arguments",
276 request->method, command->min_args);
277 } else if (params->n > command->max_args) {
278 error = xasprintf("\"%s\" command takes at most %d arguments",
279 request->method, command->max_args);
281 struct svec argv = SVEC_EMPTY_INITIALIZER;
284 svec_add(&argv, request->method);
285 for (i = 0; i < params->n; i++) {
286 if (params->elems[i]->type != JSON_STRING) {
287 error = xasprintf("\"%s\" command has non-string argument",
291 svec_add(&argv, json_string(params->elems[i]));
293 svec_terminate(&argv);
296 command->cb(conn, argv.n, (const char **) argv.names,
304 unixctl_command_reply_error(conn, error);
310 run_connection(struct unixctl_conn *conn)
314 jsonrpc_run(conn->rpc);
315 error = jsonrpc_get_status(conn->rpc);
316 if (error || jsonrpc_get_backlog(conn->rpc)) {
320 for (i = 0; i < 10; i++) {
321 struct jsonrpc_msg *msg;
323 if (error || conn->request_id) {
327 jsonrpc_recv(conn->rpc, &msg);
329 if (msg->type == JSONRPC_REQUEST) {
330 process_command(conn, msg);
332 VLOG_WARN_RL(&rl, "%s: received unexpected %s message",
333 jsonrpc_get_name(conn->rpc),
334 jsonrpc_msg_type_to_string(msg->type));
337 jsonrpc_msg_destroy(msg);
339 error = error ? error : jsonrpc_get_status(conn->rpc);
346 kill_connection(struct unixctl_conn *conn)
348 list_remove(&conn->node);
349 jsonrpc_close(conn->rpc);
350 json_destroy(conn->request_id);
355 unixctl_server_run(struct unixctl_server *server)
357 struct unixctl_conn *conn, *next;
364 for (i = 0; i < 10; i++) {
365 struct stream *stream;
368 error = pstream_accept(server->listener, &stream);
370 struct unixctl_conn *conn = xzalloc(sizeof *conn);
371 list_push_back(&server->conns, &conn->node);
372 conn->rpc = jsonrpc_open(stream);
373 } else if (error == EAGAIN) {
376 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
377 pstream_get_name(server->listener),
378 ovs_strerror(error));
382 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
383 int error = run_connection(conn);
384 if (error && error != EAGAIN) {
385 kill_connection(conn);
391 unixctl_server_wait(struct unixctl_server *server)
393 struct unixctl_conn *conn;
399 pstream_wait(server->listener);
400 LIST_FOR_EACH (conn, node, &server->conns) {
401 jsonrpc_wait(conn->rpc);
402 if (!jsonrpc_get_backlog(conn->rpc)) {
403 jsonrpc_recv_wait(conn->rpc);
408 /* Destroys 'server' and stops listening for connections. */
410 unixctl_server_destroy(struct unixctl_server *server)
413 struct unixctl_conn *conn, *next;
415 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
416 kill_connection(conn);
419 pstream_close(server->listener);
424 /* On POSIX based systems, connects to a unixctl server socket. 'path' should
425 * be the name of a unixctl server socket. If it does not start with '/', it
426 * will be prefixed with the rundir (e.g. /usr/local/var/run/openvswitch).
428 * On Windows, connects to a localhost TCP port as written inside 'path'.
429 * 'path' should be an absolute path of the file.
431 * Returns 0 if successful, otherwise a positive errno value. If successful,
432 * sets '*client' to the new jsonrpc, otherwise to NULL. */
434 unixctl_client_create(const char *path, struct jsonrpc **client)
436 char *abs_path, *unix_path;
437 struct stream *stream;
441 abs_path = strdup(path);
443 abs_path = abs_file_name(ovs_rundir(), path);
445 unix_path = xasprintf("unix:%s", abs_path);
449 error = stream_open_block(stream_open(unix_path, &stream, DSCP_DEFAULT),
455 VLOG_WARN("failed to connect to %s", path);
459 *client = jsonrpc_open(stream);
463 /* Executes 'command' on the server with an argument vector 'argv' containing
464 * 'argc' elements. If successfully communicated with the server, returns 0
465 * and sets '*result', or '*err' (not both) to the result or error the server
466 * returned. Otherwise, sets '*result' and '*err' to NULL and returns a
467 * positive errno value. The caller is responsible for freeing '*result' or
468 * '*err' if not NULL. */
470 unixctl_client_transact(struct jsonrpc *client, const char *command, int argc,
471 char *argv[], char **result, char **err)
473 struct jsonrpc_msg *request, *reply;
474 struct json **json_args, *params;
480 json_args = xmalloc(argc * sizeof *json_args);
481 for (i = 0; i < argc; i++) {
482 json_args[i] = json_string_create(argv[i]);
484 params = json_array_create(json_args, argc);
485 request = jsonrpc_create_request(command, params, NULL);
487 error = jsonrpc_transact_block(client, request, &reply);
489 VLOG_WARN("error communicating with %s: %s", jsonrpc_get_name(client),
490 ovs_retval_to_string(error));
495 if (reply->error->type == JSON_STRING) {
496 *err = xstrdup(json_string(reply->error));
498 VLOG_WARN("%s: unexpected error type in JSON RPC reply: %s",
499 jsonrpc_get_name(client),
500 json_type_to_string(reply->error->type));
503 } else if (reply->result) {
504 if (reply->result->type == JSON_STRING) {
505 *result = xstrdup(json_string(reply->result));
507 VLOG_WARN("%s: unexpected result type in JSON rpc reply: %s",
508 jsonrpc_get_name(client),
509 json_type_to_string(reply->result->type));
514 jsonrpc_msg_destroy(reply);