]> git.proxmox.com Git - mirror_ovs.git/blame - lib/unixctl.c
Replace most uses of assert by ovs_assert.
[mirror_ovs.git] / lib / unixctl.c
CommitLineData
064af421 1/*
e0edde6f 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "unixctl.h"
064af421 19#include <errno.h>
064af421
BP
20#include <unistd.h>
21#include "coverage.h"
22#include "dirs.h"
23#include "dynamic-string.h"
bde9f75d
EJ
24#include "json.h"
25#include "jsonrpc.h"
064af421 26#include "list.h"
064af421
BP
27#include "poll-loop.h"
28#include "shash.h"
bde9f75d 29#include "stream.h"
3c442619 30#include "svec.h"
5136ce49 31#include "vlog.h"
064af421 32
d98e6007 33VLOG_DEFINE_THIS_MODULE(unixctl);
d76f09ea
BP
34
35COVERAGE_DEFINE(unixctl_received);
36COVERAGE_DEFINE(unixctl_replied);
064af421
BP
37\f
38struct unixctl_command {
0e15264f
BP
39 const char *usage;
40 int min_args, max_args;
8ca79daa
BP
41 unixctl_cb_func *cb;
42 void *aux;
064af421
BP
43};
44
45struct unixctl_conn {
46 struct list node;
bde9f75d 47 struct jsonrpc *rpc;
064af421 48
bde9f75d
EJ
49 /* Only one request can be in progress at a time. While the request is
50 * being processed, 'request_id' is populated, otherwise it is null. */
51 struct json *request_id; /* ID of the currently active request. */
064af421
BP
52};
53
54/* Server for control connection. */
55struct unixctl_server {
bde9f75d 56 struct pstream *listener;
064af421
BP
57 struct list conns;
58};
59
064af421
BP
60static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
61
62static struct shash commands = SHASH_INITIALIZER(&commands);
63
64static void
0e15264f
BP
65unixctl_help(struct unixctl_conn *conn, int argc OVS_UNUSED,
66 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
064af421
BP
67{
68 struct ds ds = DS_EMPTY_INITIALIZER;
7ff2009a 69 const struct shash_node **nodes = shash_sort(&commands);
3c442619 70 size_t i;
064af421
BP
71
72 ds_put_cstr(&ds, "The available commands are:\n");
3c442619 73
7ff2009a
JP
74 for (i = 0; i < shash_count(&commands); i++) {
75 const struct shash_node *node = nodes[i];
76 const struct unixctl_command *command = node->data;
bde9f75d 77
e7b5947a 78 ds_put_format(&ds, " %-23s %s\n", node->name, command->usage);
3c442619 79 }
7ff2009a 80 free(nodes);
3c442619 81
bde9f75d 82 unixctl_command_reply(conn, ds_cstr(&ds));
064af421
BP
83 ds_destroy(&ds);
84}
85
d5e1e5ed 86static void
0e15264f
BP
87unixctl_version(struct unixctl_conn *conn, int argc OVS_UNUSED,
88 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
d5e1e5ed 89{
bde9f75d 90 unixctl_command_reply(conn, get_program_version());
d5e1e5ed
JP
91}
92
0e15264f
BP
93/* Registers a unixctl command with the given 'name'. 'usage' describes the
94 * arguments to the command; it is used only for presentation to the user in
95 * "help" output.
96 *
b558fd90
BP
97 * 'cb' is called when the command is received. It is passed an array
98 * containing the command name and arguments, plus a copy of 'aux'. Normally
99 * 'cb' should reply by calling unixctl_command_reply() or
100 * unixctl_command_reply_error() before it returns, but if the command cannot
101 * be handled immediately then it can defer the reply until later. A given
102 * connection can only process a single request at a time, so a reply must be
103 * made eventually to avoid blocking that connection. */
064af421 104void
0e15264f
BP
105unixctl_command_register(const char *name, const char *usage,
106 int min_args, int max_args,
107 unixctl_cb_func *cb, void *aux)
064af421
BP
108{
109 struct unixctl_command *command;
4895c701
SH
110 struct unixctl_command *lookup = shash_find_data(&commands, name);
111
cb22974d 112 ovs_assert(!lookup || lookup->cb == cb);
4895c701
SH
113
114 if (lookup) {
115 return;
116 }
064af421 117
064af421 118 command = xmalloc(sizeof *command);
0e15264f
BP
119 command->usage = usage;
120 command->min_args = min_args;
121 command->max_args = max_args;
064af421 122 command->cb = cb;
8ca79daa 123 command->aux = aux;
064af421
BP
124 shash_add(&commands, name, command);
125}
126
bde9f75d
EJ
127static void
128unixctl_command_reply__(struct unixctl_conn *conn,
129 bool success, const char *body)
064af421 130{
bde9f75d
EJ
131 struct json *body_json;
132 struct jsonrpc_msg *reply;
133
134 COVERAGE_INC(unixctl_replied);
cb22974d 135 ovs_assert(conn->request_id);
bde9f75d
EJ
136
137 if (!body) {
138 body = "";
064af421 139 }
bde9f75d
EJ
140
141 if (body[0] && body[strlen(body) - 1] != '\n') {
142 body_json = json_string_create_nocopy(xasprintf("%s\n", body));
143 } else {
144 body_json = json_string_create(body);
145 }
146
147 if (success) {
148 reply = jsonrpc_create_reply(body_json, conn->request_id);
149 } else {
150 reply = jsonrpc_create_error(body_json, conn->request_id);
151 }
152
153 /* If jsonrpc_send() returns an error, the run loop will take care of the
154 * problem eventually. */
155 jsonrpc_send(conn->rpc, reply);
156 json_destroy(conn->request_id);
157 conn->request_id = NULL;
064af421
BP
158}
159
bde9f75d
EJ
160/* Replies to the active unixctl connection 'conn'. 'result' is sent to the
161 * client indicating the command was processed successfully. Only one call to
162 * unixctl_command_reply() or unixctl_command_reply_error() may be made per
163 * request. */
064af421 164void
bde9f75d 165unixctl_command_reply(struct unixctl_conn *conn, const char *result)
064af421 166{
bde9f75d
EJ
167 unixctl_command_reply__(conn, true, result);
168}
064af421 169
bde9f75d
EJ
170/* Replies to the active unixctl connection 'conn'. 'error' is sent to the
171 * client indicating an error occured processing the command. Only one call to
172 * unixctl_command_reply() or unixctl_command_reply_error() may be made per
173 * request. */
174void
175unixctl_command_reply_error(struct unixctl_conn *conn, const char *error)
176{
177 unixctl_command_reply__(conn, false, error);
064af421
BP
178}
179
180/* Creates a unixctl server listening on 'path', which may be:
181 *
182 * - NULL, in which case <rundir>/<program>.<pid>.ctl is used.
183 *
614c4892
BP
184 * - "none", in which case the function will return successfully but
185 * no socket will actually be created.
186 *
064af421
BP
187 * - A name that does not start with '/', in which case it is put in
188 * <rundir>.
189 *
190 * - An absolute path (starting with '/') that gives the exact name of
191 * the Unix domain socket to listen on.
192 *
193 * A program that (optionally) daemonizes itself should call this function
194 * *after* daemonization, so that the socket name contains the pid of the
195 * daemon instead of the pid of the program that exited. (Otherwise,
3fbe1d30 196 * "ovs-appctl --target=<program>" will fail.)
064af421
BP
197 *
198 * Returns 0 if successful, otherwise a positive errno value. If successful,
614c4892
BP
199 * sets '*serverp' to the new unixctl_server (or to NULL if 'path' was "none"),
200 * otherwise to NULL. */
064af421
BP
201int
202unixctl_server_create(const char *path, struct unixctl_server **serverp)
203{
204 struct unixctl_server *server;
bde9f75d
EJ
205 struct pstream *listener;
206 char *punix_path;
064af421
BP
207 int error;
208
bde9f75d 209 *serverp = NULL;
614c4892 210 if (path && !strcmp(path, "none")) {
614c4892
BP
211 return 0;
212 }
213
064af421 214 if (path) {
bde9f75d
EJ
215 char *abs_path = abs_file_name(ovs_rundir(), path);
216 punix_path = xasprintf("punix:%s", abs_path);
217 free(abs_path);
064af421 218 } else {
bde9f75d
EJ
219 punix_path = xasprintf("punix:%s/%s.%ld.ctl", ovs_rundir(),
220 program_name, (long int) getpid());
064af421
BP
221 }
222
ef8a3d14 223 error = pstream_open(punix_path, &listener, 0);
bde9f75d 224 if (error) {
324f0c59
BP
225 ovs_error(error, "could not initialize control socket %s", punix_path);
226 goto exit;
064af421
BP
227 }
228
bde9f75d
EJ
229 unixctl_command_register("help", "", 0, 0, unixctl_help, NULL);
230 unixctl_command_register("version", "", 0, 0, unixctl_version, NULL);
064af421 231
bde9f75d
EJ
232 server = xmalloc(sizeof *server);
233 server->listener = listener;
234 list_init(&server->conns);
064af421 235 *serverp = server;
324f0c59
BP
236
237exit:
238 free(punix_path);
239 return error;
064af421
BP
240}
241
242static void
bde9f75d 243process_command(struct unixctl_conn *conn, struct jsonrpc_msg *request)
064af421 244{
bde9f75d 245 char *error = NULL;
064af421 246
064af421 247 struct unixctl_command *command;
bde9f75d 248 struct json_array *params;
064af421
BP
249
250 COVERAGE_INC(unixctl_received);
bde9f75d
EJ
251 conn->request_id = json_clone(request->id);
252
253 params = json_array(request->params);
254 command = shash_find_data(&commands, request->method);
255 if (!command) {
256 error = xasprintf("\"%s\" is not a valid command", request->method);
257 } else if (params->n < command->min_args) {
258 error = xasprintf("\"%s\" command requires at least %d arguments",
259 request->method, command->min_args);
260 } else if (params->n > command->max_args) {
261 error = xasprintf("\"%s\" command takes at most %d arguments",
262 request->method, command->max_args);
064af421 263 } else {
bde9f75d
EJ
264 struct svec argv = SVEC_EMPTY_INITIALIZER;
265 int i;
266
267 svec_add(&argv, request->method);
268 for (i = 0; i < params->n; i++) {
269 if (params->elems[i]->type != JSON_STRING) {
270 error = xasprintf("\"%s\" command has non-string argument",
271 request->method);
272 break;
273 }
274 svec_add(&argv, json_string(params->elems[i]));
275 }
276 svec_terminate(&argv);
277
278 if (!error) {
0e15264f
BP
279 command->cb(conn, argv.n, (const char **) argv.names,
280 command->aux);
281 }
282
bde9f75d 283 svec_destroy(&argv);
064af421 284 }
0e15264f 285
bde9f75d
EJ
286 if (error) {
287 unixctl_command_reply_error(conn, error);
288 free(error);
289 }
064af421
BP
290}
291
292static int
bde9f75d 293run_connection(struct unixctl_conn *conn)
064af421 294{
bde9f75d 295 int error, i;
064af421 296
bde9f75d
EJ
297 jsonrpc_run(conn->rpc);
298 error = jsonrpc_get_status(conn->rpc);
299 if (error || jsonrpc_get_backlog(conn->rpc)) {
300 return error;
301 }
064af421 302
bde9f75d
EJ
303 for (i = 0; i < 10; i++) {
304 struct jsonrpc_msg *msg;
064af421 305
bde9f75d
EJ
306 if (error || conn->request_id) {
307 break;
064af421
BP
308 }
309
bde9f75d
EJ
310 jsonrpc_recv(conn->rpc, &msg);
311 if (msg) {
312 if (msg->type == JSONRPC_REQUEST) {
313 process_command(conn, msg);
064af421 314 } else {
bde9f75d
EJ
315 VLOG_WARN_RL(&rl, "%s: received unexpected %s message",
316 jsonrpc_get_name(conn->rpc),
317 jsonrpc_msg_type_to_string(msg->type));
318 error = EINVAL;
064af421 319 }
bde9f75d 320 jsonrpc_msg_destroy(msg);
064af421 321 }
bde9f75d 322 error = error ? error : jsonrpc_get_status(conn->rpc);
064af421 323 }
064af421 324
bde9f75d 325 return error;
064af421
BP
326}
327
328static void
329kill_connection(struct unixctl_conn *conn)
330{
331 list_remove(&conn->node);
bde9f75d
EJ
332 jsonrpc_close(conn->rpc);
333 json_destroy(conn->request_id);
064af421
BP
334 free(conn);
335}
336
337void
338unixctl_server_run(struct unixctl_server *server)
339{
340 struct unixctl_conn *conn, *next;
341 int i;
342
614c4892
BP
343 if (!server) {
344 return;
345 }
346
064af421 347 for (i = 0; i < 10; i++) {
bde9f75d
EJ
348 struct stream *stream;
349 int error;
350
351 error = pstream_accept(server->listener, &stream);
352 if (!error) {
353 struct unixctl_conn *conn = xzalloc(sizeof *conn);
354 list_push_back(&server->conns, &conn->node);
355 conn->rpc = jsonrpc_open(stream);
356 } else if (error == EAGAIN) {
064af421 357 break;
bde9f75d
EJ
358 } else {
359 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
360 pstream_get_name(server->listener),
361 strerror(error));
064af421 362 }
064af421
BP
363 }
364
4e8e4213 365 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
064af421
BP
366 int error = run_connection(conn);
367 if (error && error != EAGAIN) {
368 kill_connection(conn);
369 }
370 }
371}
372
373void
374unixctl_server_wait(struct unixctl_server *server)
375{
376 struct unixctl_conn *conn;
377
614c4892
BP
378 if (!server) {
379 return;
380 }
381
bde9f75d 382 pstream_wait(server->listener);
4e8e4213 383 LIST_FOR_EACH (conn, node, &server->conns) {
bde9f75d
EJ
384 jsonrpc_wait(conn->rpc);
385 if (!jsonrpc_get_backlog(conn->rpc)) {
386 jsonrpc_recv_wait(conn->rpc);
064af421
BP
387 }
388 }
389}
390
391/* Destroys 'server' and stops listening for connections. */
392void
393unixctl_server_destroy(struct unixctl_server *server)
394{
395 if (server) {
396 struct unixctl_conn *conn, *next;
397
4e8e4213 398 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
064af421
BP
399 kill_connection(conn);
400 }
401
bde9f75d 402 pstream_close(server->listener);
064af421
BP
403 free(server);
404 }
405}
406\f
bde9f75d
EJ
407/* Connects to a unixctl server socket. 'path' should be the name of a unixctl
408 * server socket. If it does not start with '/', it will be prefixed with the
409 * rundir (e.g. /usr/local/var/run/openvswitch).
064af421
BP
410 *
411 * Returns 0 if successful, otherwise a positive errno value. If successful,
bde9f75d 412 * sets '*client' to the new jsonrpc, otherwise to NULL. */
064af421 413int
bde9f75d 414unixctl_client_create(const char *path, struct jsonrpc **client)
064af421 415{
bde9f75d
EJ
416 char *abs_path, *unix_path;
417 struct stream *stream;
064af421 418 int error;
064af421 419
bde9f75d 420 *client = NULL;
064af421 421
bde9f75d
EJ
422 abs_path = abs_file_name(ovs_rundir(), path);
423 unix_path = xasprintf("unix:%s", abs_path);
f125905c
MM
424 error = stream_open_block(stream_open(unix_path, &stream, DSCP_DEFAULT),
425 &stream);
bde9f75d
EJ
426 free(unix_path);
427 free(abs_path);
064af421 428
bde9f75d
EJ
429 if (error) {
430 VLOG_WARN("failed to connect to %s", path);
431 return error;
064af421 432 }
bde9f75d
EJ
433
434 *client = jsonrpc_open(stream);
435 return 0;
064af421
BP
436}
437
bde9f75d
EJ
438/* Executes 'command' on the server with an argument vector 'argv' containing
439 * 'argc' elements. If successfully communicated with the server, returns 0
440 * and sets '*result', or '*err' (not both) to the result or error the server
441 * returned. Otherwise, sets '*result' and '*err' to NULL and returns a
442 * positive errno value. The caller is responsible for freeing '*result' or
443 * '*err' if not NULL. */
064af421 444int
bde9f75d
EJ
445unixctl_client_transact(struct jsonrpc *client, const char *command, int argc,
446 char *argv[], char **result, char **err)
064af421 447{
bde9f75d
EJ
448 struct jsonrpc_msg *request, *reply;
449 struct json **json_args, *params;
450 int error, i;
064af421 451
bde9f75d
EJ
452 *result = NULL;
453 *err = NULL;
454
455 json_args = xmalloc(argc * sizeof *json_args);
456 for (i = 0; i < argc; i++) {
457 json_args[i] = json_string_create(argv[i]);
064af421 458 }
bde9f75d
EJ
459 params = json_array_create(json_args, argc);
460 request = jsonrpc_create_request(command, params, NULL);
461
462 error = jsonrpc_transact_block(client, request, &reply);
463 if (error) {
464 VLOG_WARN("error communicating with %s: %s", jsonrpc_get_name(client),
465 strerror(error));
466 return error;
064af421
BP
467 }
468
bde9f75d
EJ
469 if (reply->error) {
470 if (reply->error->type == JSON_STRING) {
471 *err = xstrdup(json_string(reply->error));
472 } else {
473 VLOG_WARN("%s: unexpected error type in JSON RPC reply: %s",
474 jsonrpc_get_name(client),
475 json_type_to_string(reply->error->type));
476 error = EINVAL;
064af421 477 }
bde9f75d
EJ
478 } else if (reply->result) {
479 if (reply->result->type == JSON_STRING) {
480 *result = xstrdup(json_string(reply->result));
064af421 481 } else {
bde9f75d
EJ
482 VLOG_WARN("%s: unexpected result type in JSON rpc reply: %s",
483 jsonrpc_get_name(client),
484 json_type_to_string(reply->result->type));
485 error = EINVAL;
064af421
BP
486 }
487 }
064af421 488
bde9f75d
EJ
489 jsonrpc_msg_destroy(reply);
490 return error;
064af421 491}