]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/unixctl.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 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 "openvswitch/dynamic-string.h"
24 #include "openvswitch/json.h"
26 #include "openvswitch/list.h"
27 #include "openvswitch/poll-loop.h"
28 #include "openvswitch/shash.h"
30 #include "stream-provider.h"
32 #include "openvswitch/vlog.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
;
58 struct ovs_list conns
;
62 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
64 static struct shash commands
= SHASH_INITIALIZER(&commands
);
67 unixctl_list_commands(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
68 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
70 struct ds ds
= DS_EMPTY_INITIALIZER
;
71 const struct shash_node
**nodes
= shash_sort(&commands
);
74 ds_put_cstr(&ds
, "The available commands are:\n");
76 for (i
= 0; i
< shash_count(&commands
); i
++) {
77 const struct shash_node
*node
= nodes
[i
];
78 const struct unixctl_command
*command
= node
->data
;
81 ds_put_format(&ds
, " %-23s %s\n", node
->name
, command
->usage
);
86 unixctl_command_reply(conn
, ds_cstr(&ds
));
91 unixctl_version(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
92 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
94 unixctl_command_reply(conn
, ovs_get_program_version());
97 /* Registers a unixctl command with the given 'name'. 'usage' describes the
98 * arguments to the command; it is used only for presentation to the user in
99 * "list-commands" output. (If 'usage' is NULL, then the command is hidden.)
101 * 'cb' is called when the command is received. It is passed an array
102 * containing the command name and arguments, plus a copy of 'aux'. Normally
103 * 'cb' should reply by calling unixctl_command_reply() or
104 * unixctl_command_reply_error() before it returns, but if the command cannot
105 * be handled immediately then it can defer the reply until later. A given
106 * connection can only process a single request at a time, so a reply must be
107 * made eventually to avoid blocking that connection. */
109 unixctl_command_register(const char *name
, const char *usage
,
110 int min_args
, int max_args
,
111 unixctl_cb_func
*cb
, void *aux
)
113 struct unixctl_command
*command
;
114 struct unixctl_command
*lookup
= shash_find_data(&commands
, name
);
116 ovs_assert(!lookup
|| lookup
->cb
== cb
);
122 command
= xmalloc(sizeof *command
);
123 command
->usage
= usage
;
124 command
->min_args
= min_args
;
125 command
->max_args
= max_args
;
128 shash_add(&commands
, name
, command
);
132 unixctl_command_reply__(struct unixctl_conn
*conn
,
133 bool success
, const char *body
)
135 struct json
*body_json
;
136 struct jsonrpc_msg
*reply
;
138 COVERAGE_INC(unixctl_replied
);
139 ovs_assert(conn
->request_id
);
145 if (body
[0] && body
[strlen(body
) - 1] != '\n') {
146 body_json
= json_string_create_nocopy(xasprintf("%s\n", body
));
148 body_json
= json_string_create(body
);
152 reply
= jsonrpc_create_reply(body_json
, conn
->request_id
);
154 reply
= jsonrpc_create_error(body_json
, conn
->request_id
);
157 if (VLOG_IS_DBG_ENABLED()) {
158 char *id
= json_to_string(conn
->request_id
, 0);
159 VLOG_DBG("replying with %s, id=%s: \"%s\"",
160 success
? "success" : "error", id
, body
);
164 /* If jsonrpc_send() returns an error, the run loop will take care of the
165 * problem eventually. */
166 jsonrpc_send(conn
->rpc
, reply
);
167 json_destroy(conn
->request_id
);
168 conn
->request_id
= NULL
;
171 /* Replies to the active unixctl connection 'conn'. 'result' is sent to the
172 * client indicating the command was processed successfully. Only one call to
173 * unixctl_command_reply() or unixctl_command_reply_error() may be made per
176 unixctl_command_reply(struct unixctl_conn
*conn
, const char *result
)
178 unixctl_command_reply__(conn
, true, result
);
181 /* Replies to the active unixctl connection 'conn'. 'error' is sent to the
182 * client indicating an error occurred processing the command. Only one call to
183 * unixctl_command_reply() or unixctl_command_reply_error() may be made per
186 unixctl_command_reply_error(struct unixctl_conn
*conn
, const char *error
)
188 unixctl_command_reply__(conn
, false, error
);
191 /* Creates a unixctl server listening on 'path', which for POSIX may be:
193 * - NULL, in which case <rundir>/<program>.<pid>.ctl is used.
195 * - A name that does not start with '/', in which case it is put in
198 * - An absolute path (starting with '/') that gives the exact name of
199 * the Unix domain socket to listen on.
201 * For Windows, a local named pipe is used. A file is created in 'path'
204 * - NULL, in which case <rundir>/<program>.ctl is used.
206 * - An absolute path that gives the name of the file.
208 * For both POSIX and Windows, if the path is "none", the function will
209 * return successfully but no socket will actually be created.
211 * A program that (optionally) daemonizes itself should call this function
212 * *after* daemonization, so that the socket name contains the pid of the
213 * daemon instead of the pid of the program that exited. (Otherwise,
214 * "ovs-appctl --target=<program>" will fail.)
216 * Returns 0 if successful, otherwise a positive errno value. If successful,
217 * sets '*serverp' to the new unixctl_server (or to NULL if 'path' was "none"),
218 * otherwise to NULL. */
220 unixctl_server_create(const char *path
, struct unixctl_server
**serverp
)
223 if (path
&& !strcmp(path
, "none")) {
228 enum { WINDOWS
= 1 };
230 enum { WINDOWS
= 0 };
233 long int pid
= getpid();
235 = (path
? abs_file_name(ovs_rundir(), path
)
236 : WINDOWS
? xasprintf("%s/%s.ctl", ovs_rundir(), program_name
)
237 : xasprintf("%s/%s.%ld.ctl", ovs_rundir(), program_name
, pid
));
239 struct pstream
*listener
;
240 char *punix_path
= xasprintf("punix:%s", abs_path
);
241 int error
= pstream_open(punix_path
, &listener
, 0);
245 ovs_error(error
, "%s: could not initialize control socket", abs_path
);
250 unixctl_command_register("list-commands", "", 0, 0, unixctl_list_commands
,
252 unixctl_command_register("version", "", 0, 0, unixctl_version
, NULL
);
254 struct unixctl_server
*server
= xmalloc(sizeof *server
);
255 server
->listener
= listener
;
256 server
->path
= abs_path
;
257 ovs_list_init(&server
->conns
);
263 process_command(struct unixctl_conn
*conn
, struct jsonrpc_msg
*request
)
267 struct unixctl_command
*command
;
268 struct json_array
*params
;
270 COVERAGE_INC(unixctl_received
);
271 conn
->request_id
= json_clone(request
->id
);
273 if (VLOG_IS_DBG_ENABLED()) {
274 char *params_s
= json_to_string(request
->params
, 0);
275 char *id_s
= json_to_string(request
->id
, 0);
276 VLOG_DBG("received request %s%s, id=%s",
277 request
->method
, params_s
, id_s
);
282 params
= json_array(request
->params
);
283 command
= shash_find_data(&commands
, request
->method
);
285 error
= xasprintf("\"%s\" is not a valid command (use "
286 "\"list-commands\" to see a list of valid commands)",
288 } else if (params
->n
< command
->min_args
) {
289 error
= xasprintf("\"%s\" command requires at least %d arguments",
290 request
->method
, command
->min_args
);
291 } else if (params
->n
> command
->max_args
) {
292 error
= xasprintf("\"%s\" command takes at most %d arguments",
293 request
->method
, command
->max_args
);
295 struct svec argv
= SVEC_EMPTY_INITIALIZER
;
298 svec_add(&argv
, request
->method
);
299 for (i
= 0; i
< params
->n
; i
++) {
300 if (params
->elems
[i
]->type
!= JSON_STRING
) {
301 error
= xasprintf("\"%s\" command has non-string argument",
305 svec_add(&argv
, json_string(params
->elems
[i
]));
307 svec_terminate(&argv
);
310 command
->cb(conn
, argv
.n
, (const char **) argv
.names
,
318 unixctl_command_reply_error(conn
, error
);
324 run_connection(struct unixctl_conn
*conn
)
328 jsonrpc_run(conn
->rpc
);
329 error
= jsonrpc_get_status(conn
->rpc
);
330 if (error
|| jsonrpc_get_backlog(conn
->rpc
)) {
334 for (i
= 0; i
< 10; i
++) {
335 struct jsonrpc_msg
*msg
;
337 if (error
|| conn
->request_id
) {
341 jsonrpc_recv(conn
->rpc
, &msg
);
343 if (msg
->type
== JSONRPC_REQUEST
) {
344 process_command(conn
, msg
);
346 VLOG_WARN_RL(&rl
, "%s: received unexpected %s message",
347 jsonrpc_get_name(conn
->rpc
),
348 jsonrpc_msg_type_to_string(msg
->type
));
351 jsonrpc_msg_destroy(msg
);
353 error
= error
? error
: jsonrpc_get_status(conn
->rpc
);
360 kill_connection(struct unixctl_conn
*conn
)
362 ovs_list_remove(&conn
->node
);
363 jsonrpc_close(conn
->rpc
);
364 json_destroy(conn
->request_id
);
369 unixctl_server_run(struct unixctl_server
*server
)
375 for (int i
= 0; i
< 10; i
++) {
376 struct stream
*stream
;
379 error
= pstream_accept(server
->listener
, &stream
);
381 struct unixctl_conn
*conn
= xzalloc(sizeof *conn
);
382 ovs_list_push_back(&server
->conns
, &conn
->node
);
383 conn
->rpc
= jsonrpc_open(stream
);
384 } else if (error
== EAGAIN
) {
387 VLOG_WARN_RL(&rl
, "%s: accept failed: %s",
388 pstream_get_name(server
->listener
),
389 ovs_strerror(error
));
393 struct unixctl_conn
*conn
, *next
;
394 LIST_FOR_EACH_SAFE (conn
, next
, node
, &server
->conns
) {
395 int error
= run_connection(conn
);
396 if (error
&& error
!= EAGAIN
) {
397 kill_connection(conn
);
403 unixctl_server_wait(struct unixctl_server
*server
)
405 struct unixctl_conn
*conn
;
411 pstream_wait(server
->listener
);
412 LIST_FOR_EACH (conn
, node
, &server
->conns
) {
413 jsonrpc_wait(conn
->rpc
);
414 if (!jsonrpc_get_backlog(conn
->rpc
) && !conn
->request_id
) {
415 jsonrpc_recv_wait(conn
->rpc
);
420 /* Destroys 'server' and stops listening for connections. */
422 unixctl_server_destroy(struct unixctl_server
*server
)
425 struct unixctl_conn
*conn
, *next
;
427 LIST_FOR_EACH_SAFE (conn
, next
, node
, &server
->conns
) {
428 kill_connection(conn
);
432 pstream_close(server
->listener
);
438 unixctl_server_get_path(const struct unixctl_server
*server
)
440 return server
? server
->path
: NULL
;
443 /* On POSIX based systems, connects to a unixctl server socket. 'path' should
444 * be the name of a unixctl server socket. If it does not start with '/', it
445 * will be prefixed with the rundir (e.g. /usr/local/var/run/openvswitch).
447 * On Windows, connects to a local named pipe. A file which resides in
448 * 'path' is used to mimic the behavior of a Unix domain socket.
449 * 'path' should be an absolute path of the file.
451 * Returns 0 if successful, otherwise a positive errno value. If successful,
452 * sets '*client' to the new jsonrpc, otherwise to NULL. */
454 unixctl_client_create(const char *path
, struct jsonrpc
**client
)
456 struct stream
*stream
;
459 char *abs_path
= abs_file_name(ovs_rundir(), path
);
460 char *unix_path
= xasprintf("unix:%s", abs_path
);
464 error
= stream_open_block(stream_open(unix_path
, &stream
, DSCP_DEFAULT
),
470 VLOG_WARN("failed to connect to %s", path
);
474 *client
= jsonrpc_open(stream
);
478 /* Executes 'command' on the server with an argument vector 'argv' containing
479 * 'argc' elements. If successfully communicated with the server, returns 0
480 * and sets '*result', or '*err' (not both) to the result or error the server
481 * returned. Otherwise, sets '*result' and '*err' to NULL and returns a
482 * positive errno value. The caller is responsible for freeing '*result' or
483 * '*err' if not NULL. */
485 unixctl_client_transact(struct jsonrpc
*client
, const char *command
, int argc
,
486 char *argv
[], char **result
, char **err
)
488 struct jsonrpc_msg
*request
, *reply
;
489 struct json
**json_args
, *params
;
495 json_args
= xmalloc(argc
* sizeof *json_args
);
496 for (i
= 0; i
< argc
; i
++) {
497 json_args
[i
] = json_string_create(argv
[i
]);
499 params
= json_array_create(json_args
, argc
);
500 request
= jsonrpc_create_request(command
, params
, NULL
);
502 error
= jsonrpc_transact_block(client
, request
, &reply
);
504 VLOG_WARN("error communicating with %s: %s", jsonrpc_get_name(client
),
505 ovs_retval_to_string(error
));
510 if (reply
->error
->type
== JSON_STRING
) {
511 *err
= xstrdup(json_string(reply
->error
));
513 VLOG_WARN("%s: unexpected error type in JSON RPC reply: %s",
514 jsonrpc_get_name(client
),
515 json_type_to_string(reply
->error
->type
));
518 } else if (reply
->result
) {
519 if (reply
->result
->type
== JSON_STRING
) {
520 *result
= xstrdup(json_string(reply
->result
));
522 VLOG_WARN("%s: unexpected result type in JSON rpc reply: %s",
523 jsonrpc_get_name(client
),
524 json_type_to_string(reply
->result
->type
));
529 jsonrpc_msg_destroy(reply
);