]> git.proxmox.com Git - mirror_ovs.git/blame - lib/unixctl.c
tests: Define a variable "IS_WIN32" for tests.
[mirror_ovs.git] / lib / unixctl.c
CommitLineData
064af421 1/*
2bf1d3cc 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
cb54a8c5 30#include "stream-provider.h"
3c442619 31#include "svec.h"
5136ce49 32#include "vlog.h"
064af421 33
d98e6007 34VLOG_DEFINE_THIS_MODULE(unixctl);
d76f09ea
BP
35
36COVERAGE_DEFINE(unixctl_received);
37COVERAGE_DEFINE(unixctl_replied);
064af421
BP
38\f
39struct unixctl_command {
0e15264f
BP
40 const char *usage;
41 int min_args, max_args;
8ca79daa
BP
42 unixctl_cb_func *cb;
43 void *aux;
064af421
BP
44};
45
46struct unixctl_conn {
47 struct list node;
bde9f75d 48 struct jsonrpc *rpc;
064af421 49
bde9f75d
EJ
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. */
064af421
BP
53};
54
55/* Server for control connection. */
56struct unixctl_server {
bde9f75d 57 struct pstream *listener;
064af421
BP
58 struct list conns;
59};
60
064af421
BP
61static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
62
63static struct shash commands = SHASH_INITIALIZER(&commands);
64
65static void
0e15264f
BP
66unixctl_help(struct unixctl_conn *conn, int argc OVS_UNUSED,
67 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
064af421
BP
68{
69 struct ds ds = DS_EMPTY_INITIALIZER;
7ff2009a 70 const struct shash_node **nodes = shash_sort(&commands);
3c442619 71 size_t i;
064af421
BP
72
73 ds_put_cstr(&ds, "The available commands are:\n");
3c442619 74
7ff2009a
JP
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;
bde9f75d 78
e7b5947a 79 ds_put_format(&ds, " %-23s %s\n", node->name, command->usage);
3c442619 80 }
7ff2009a 81 free(nodes);
3c442619 82
bde9f75d 83 unixctl_command_reply(conn, ds_cstr(&ds));
064af421
BP
84 ds_destroy(&ds);
85}
86
d5e1e5ed 87static void
0e15264f
BP
88unixctl_version(struct unixctl_conn *conn, int argc OVS_UNUSED,
89 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
d5e1e5ed 90{
bde9f75d 91 unixctl_command_reply(conn, get_program_version());
d5e1e5ed
JP
92}
93
0e15264f
BP
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
96 * "help" output.
97 *
b558fd90
BP
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. */
064af421 105void
0e15264f
BP
106unixctl_command_register(const char *name, const char *usage,
107 int min_args, int max_args,
108 unixctl_cb_func *cb, void *aux)
064af421
BP
109{
110 struct unixctl_command *command;
4895c701
SH
111 struct unixctl_command *lookup = shash_find_data(&commands, name);
112
cb22974d 113 ovs_assert(!lookup || lookup->cb == cb);
4895c701
SH
114
115 if (lookup) {
116 return;
117 }
064af421 118
064af421 119 command = xmalloc(sizeof *command);
0e15264f
BP
120 command->usage = usage;
121 command->min_args = min_args;
122 command->max_args = max_args;
064af421 123 command->cb = cb;
8ca79daa 124 command->aux = aux;
064af421
BP
125 shash_add(&commands, name, command);
126}
127
bde9f75d
EJ
128static void
129unixctl_command_reply__(struct unixctl_conn *conn,
130 bool success, const char *body)
064af421 131{
bde9f75d
EJ
132 struct json *body_json;
133 struct jsonrpc_msg *reply;
134
135 COVERAGE_INC(unixctl_replied);
cb22974d 136 ovs_assert(conn->request_id);
bde9f75d
EJ
137
138 if (!body) {
139 body = "";
064af421 140 }
bde9f75d
EJ
141
142 if (body[0] && body[strlen(body) - 1] != '\n') {
143 body_json = json_string_create_nocopy(xasprintf("%s\n", body));
144 } else {
145 body_json = json_string_create(body);
146 }
147
148 if (success) {
149 reply = jsonrpc_create_reply(body_json, conn->request_id);
150 } else {
151 reply = jsonrpc_create_error(body_json, conn->request_id);
152 }
153
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;
064af421
BP
159}
160
bde9f75d
EJ
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
164 * request. */
064af421 165void
bde9f75d 166unixctl_command_reply(struct unixctl_conn *conn, const char *result)
064af421 167{
bde9f75d
EJ
168 unixctl_command_reply__(conn, true, result);
169}
064af421 170
bde9f75d 171/* Replies to the active unixctl connection 'conn'. 'error' is sent to the
ec9f40dc 172 * client indicating an error occurred processing the command. Only one call to
bde9f75d
EJ
173 * unixctl_command_reply() or unixctl_command_reply_error() may be made per
174 * request. */
175void
176unixctl_command_reply_error(struct unixctl_conn *conn, const char *error)
177{
178 unixctl_command_reply__(conn, false, error);
064af421
BP
179}
180
cb54a8c5 181/* Creates a unixctl server listening on 'path', which for POSIX may be:
064af421
BP
182 *
183 * - NULL, in which case <rundir>/<program>.<pid>.ctl is used.
184 *
185 * - A name that does not start with '/', in which case it is put in
186 * <rundir>.
187 *
188 * - An absolute path (starting with '/') that gives the exact name of
189 * the Unix domain socket to listen on.
190 *
cb54a8c5
GS
191 * For Windows, a kernel assigned TCP port is used and written in 'path'
192 * which may be:
193 *
194 * - NULL, in which case <rundir>/<program>.ctl is used.
195 *
196 * - An absolute path that gives the name of the file.
197 *
198 * For both POSIX and Windows, if the path is "none", the function will
199 * return successfully but no socket will actually be created.
200 *
064af421
BP
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,
3fbe1d30 204 * "ovs-appctl --target=<program>" will fail.)
064af421
BP
205 *
206 * Returns 0 if successful, otherwise a positive errno value. If successful,
614c4892
BP
207 * sets '*serverp' to the new unixctl_server (or to NULL if 'path' was "none"),
208 * otherwise to NULL. */
064af421
BP
209int
210unixctl_server_create(const char *path, struct unixctl_server **serverp)
211{
212 struct unixctl_server *server;
bde9f75d 213 struct pstream *listener;
cb54a8c5 214 char *punix_path, *abs_path = NULL;
064af421 215 int error;
cb54a8c5
GS
216#ifdef _WIN32
217 FILE *file;
218#endif
064af421 219
bde9f75d 220 *serverp = NULL;
614c4892 221 if (path && !strcmp(path, "none")) {
614c4892
BP
222 return 0;
223 }
224
cb54a8c5 225#ifndef _WIN32
064af421 226 if (path) {
cb54a8c5 227 abs_path = abs_file_name(ovs_rundir(), path);
bde9f75d 228 punix_path = xasprintf("punix:%s", abs_path);
064af421 229 } else {
bde9f75d
EJ
230 punix_path = xasprintf("punix:%s/%s.%ld.ctl", ovs_rundir(),
231 program_name, (long int) getpid());
064af421 232 }
cb54a8c5
GS
233#else
234 punix_path = xstrdup("ptcp:0:127.0.0.1");
235#endif
064af421 236
ef8a3d14 237 error = pstream_open(punix_path, &listener, 0);
bde9f75d 238 if (error) {
324f0c59
BP
239 ovs_error(error, "could not initialize control socket %s", punix_path);
240 goto exit;
064af421
BP
241 }
242
cb54a8c5
GS
243#ifdef _WIN32
244 if (path) {
245 abs_path = xstrdup(path);
246 } else {
247 abs_path = xasprintf("%s/%s.ctl", ovs_rundir(), program_name);
248 }
249
250 file = fopen(abs_path, "w");
251 if (!file) {
252 error = errno;
253 ovs_error(error, "could not open %s", abs_path);
254 goto exit;
255 }
256
257 fprintf(file, "%d\n", ntohs(listener->bound_port));
258 if (fflush(file) == EOF) {
259 error = EIO;
260 ovs_error(error, "write failed for %s", abs_path);
261 fclose(file);
262 goto exit;
263 }
264 fclose(file);
265#endif
266
bde9f75d
EJ
267 unixctl_command_register("help", "", 0, 0, unixctl_help, NULL);
268 unixctl_command_register("version", "", 0, 0, unixctl_version, NULL);
064af421 269
bde9f75d
EJ
270 server = xmalloc(sizeof *server);
271 server->listener = listener;
272 list_init(&server->conns);
064af421 273 *serverp = server;
324f0c59
BP
274
275exit:
cb54a8c5
GS
276 if (abs_path) {
277 free(abs_path);
278 }
324f0c59
BP
279 free(punix_path);
280 return error;
064af421
BP
281}
282
283static void
bde9f75d 284process_command(struct unixctl_conn *conn, struct jsonrpc_msg *request)
064af421 285{
bde9f75d 286 char *error = NULL;
064af421 287
064af421 288 struct unixctl_command *command;
bde9f75d 289 struct json_array *params;
064af421
BP
290
291 COVERAGE_INC(unixctl_received);
bde9f75d
EJ
292 conn->request_id = json_clone(request->id);
293
294 params = json_array(request->params);
295 command = shash_find_data(&commands, request->method);
296 if (!command) {
297 error = xasprintf("\"%s\" is not a valid command", request->method);
298 } else if (params->n < command->min_args) {
299 error = xasprintf("\"%s\" command requires at least %d arguments",
300 request->method, command->min_args);
301 } else if (params->n > command->max_args) {
302 error = xasprintf("\"%s\" command takes at most %d arguments",
303 request->method, command->max_args);
064af421 304 } else {
bde9f75d
EJ
305 struct svec argv = SVEC_EMPTY_INITIALIZER;
306 int i;
307
308 svec_add(&argv, request->method);
309 for (i = 0; i < params->n; i++) {
310 if (params->elems[i]->type != JSON_STRING) {
311 error = xasprintf("\"%s\" command has non-string argument",
312 request->method);
313 break;
314 }
315 svec_add(&argv, json_string(params->elems[i]));
316 }
317 svec_terminate(&argv);
318
319 if (!error) {
0e15264f
BP
320 command->cb(conn, argv.n, (const char **) argv.names,
321 command->aux);
322 }
323
bde9f75d 324 svec_destroy(&argv);
064af421 325 }
0e15264f 326
bde9f75d
EJ
327 if (error) {
328 unixctl_command_reply_error(conn, error);
329 free(error);
330 }
064af421
BP
331}
332
333static int
bde9f75d 334run_connection(struct unixctl_conn *conn)
064af421 335{
bde9f75d 336 int error, i;
064af421 337
bde9f75d
EJ
338 jsonrpc_run(conn->rpc);
339 error = jsonrpc_get_status(conn->rpc);
340 if (error || jsonrpc_get_backlog(conn->rpc)) {
341 return error;
342 }
064af421 343
bde9f75d
EJ
344 for (i = 0; i < 10; i++) {
345 struct jsonrpc_msg *msg;
064af421 346
bde9f75d
EJ
347 if (error || conn->request_id) {
348 break;
064af421
BP
349 }
350
bde9f75d
EJ
351 jsonrpc_recv(conn->rpc, &msg);
352 if (msg) {
353 if (msg->type == JSONRPC_REQUEST) {
354 process_command(conn, msg);
064af421 355 } else {
bde9f75d
EJ
356 VLOG_WARN_RL(&rl, "%s: received unexpected %s message",
357 jsonrpc_get_name(conn->rpc),
358 jsonrpc_msg_type_to_string(msg->type));
359 error = EINVAL;
064af421 360 }
bde9f75d 361 jsonrpc_msg_destroy(msg);
064af421 362 }
bde9f75d 363 error = error ? error : jsonrpc_get_status(conn->rpc);
064af421 364 }
064af421 365
bde9f75d 366 return error;
064af421
BP
367}
368
369static void
370kill_connection(struct unixctl_conn *conn)
371{
372 list_remove(&conn->node);
bde9f75d
EJ
373 jsonrpc_close(conn->rpc);
374 json_destroy(conn->request_id);
064af421
BP
375 free(conn);
376}
377
378void
379unixctl_server_run(struct unixctl_server *server)
380{
381 struct unixctl_conn *conn, *next;
382 int i;
383
614c4892
BP
384 if (!server) {
385 return;
386 }
387
064af421 388 for (i = 0; i < 10; i++) {
bde9f75d
EJ
389 struct stream *stream;
390 int error;
391
392 error = pstream_accept(server->listener, &stream);
393 if (!error) {
394 struct unixctl_conn *conn = xzalloc(sizeof *conn);
395 list_push_back(&server->conns, &conn->node);
396 conn->rpc = jsonrpc_open(stream);
397 } else if (error == EAGAIN) {
064af421 398 break;
bde9f75d
EJ
399 } else {
400 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
401 pstream_get_name(server->listener),
10a89ef0 402 ovs_strerror(error));
064af421 403 }
064af421
BP
404 }
405
4e8e4213 406 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
064af421
BP
407 int error = run_connection(conn);
408 if (error && error != EAGAIN) {
409 kill_connection(conn);
410 }
411 }
412}
413
414void
415unixctl_server_wait(struct unixctl_server *server)
416{
417 struct unixctl_conn *conn;
418
614c4892
BP
419 if (!server) {
420 return;
421 }
422
bde9f75d 423 pstream_wait(server->listener);
4e8e4213 424 LIST_FOR_EACH (conn, node, &server->conns) {
bde9f75d
EJ
425 jsonrpc_wait(conn->rpc);
426 if (!jsonrpc_get_backlog(conn->rpc)) {
427 jsonrpc_recv_wait(conn->rpc);
064af421
BP
428 }
429 }
430}
431
432/* Destroys 'server' and stops listening for connections. */
433void
434unixctl_server_destroy(struct unixctl_server *server)
435{
436 if (server) {
437 struct unixctl_conn *conn, *next;
438
4e8e4213 439 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
064af421
BP
440 kill_connection(conn);
441 }
442
bde9f75d 443 pstream_close(server->listener);
064af421
BP
444 free(server);
445 }
446}
447\f
cb54a8c5
GS
448/* On POSIX based systems, connects to a unixctl server socket. 'path' should
449 * be the name of a unixctl server socket. If it does not start with '/', it
450 * will be prefixed with the rundir (e.g. /usr/local/var/run/openvswitch).
451 *
452 * On Windows, connects to a localhost TCP port as written inside 'path'.
453 * 'path' should be an absolute path of the file.
064af421
BP
454 *
455 * Returns 0 if successful, otherwise a positive errno value. If successful,
bde9f75d 456 * sets '*client' to the new jsonrpc, otherwise to NULL. */
064af421 457int
bde9f75d 458unixctl_client_create(const char *path, struct jsonrpc **client)
064af421 459{
bde9f75d
EJ
460 char *abs_path, *unix_path;
461 struct stream *stream;
064af421 462 int error;
cb54a8c5
GS
463#ifdef _WIN32
464 FILE *file;
465 int port;
466
467 abs_path = strdup(path);
468 file = fopen(abs_path, "r");
469 if (!file) {
470 int error = errno;
471 ovs_error(error, "could not open %s", abs_path);
472 free(abs_path);
473 return error;
474 }
064af421 475
cb54a8c5
GS
476 error = fscanf(file, "%d", &port);
477 if (error != 1) {
478 ovs_error(errno, "failed to read port from %s", abs_path);
479 free(abs_path);
480 return EINVAL;
481 }
482 fclose(file);
064af421 483
cb54a8c5
GS
484 unix_path = xasprintf("tcp:127.0.0.1:%d", port);
485#else
bde9f75d
EJ
486 abs_path = abs_file_name(ovs_rundir(), path);
487 unix_path = xasprintf("unix:%s", abs_path);
cb54a8c5
GS
488#endif
489
490 *client = NULL;
491
f125905c
MM
492 error = stream_open_block(stream_open(unix_path, &stream, DSCP_DEFAULT),
493 &stream);
bde9f75d
EJ
494 free(unix_path);
495 free(abs_path);
064af421 496
bde9f75d
EJ
497 if (error) {
498 VLOG_WARN("failed to connect to %s", path);
499 return error;
064af421 500 }
bde9f75d
EJ
501
502 *client = jsonrpc_open(stream);
503 return 0;
064af421
BP
504}
505
bde9f75d
EJ
506/* Executes 'command' on the server with an argument vector 'argv' containing
507 * 'argc' elements. If successfully communicated with the server, returns 0
508 * and sets '*result', or '*err' (not both) to the result or error the server
509 * returned. Otherwise, sets '*result' and '*err' to NULL and returns a
510 * positive errno value. The caller is responsible for freeing '*result' or
511 * '*err' if not NULL. */
064af421 512int
bde9f75d
EJ
513unixctl_client_transact(struct jsonrpc *client, const char *command, int argc,
514 char *argv[], char **result, char **err)
064af421 515{
bde9f75d
EJ
516 struct jsonrpc_msg *request, *reply;
517 struct json **json_args, *params;
518 int error, i;
064af421 519
bde9f75d
EJ
520 *result = NULL;
521 *err = NULL;
522
523 json_args = xmalloc(argc * sizeof *json_args);
524 for (i = 0; i < argc; i++) {
525 json_args[i] = json_string_create(argv[i]);
064af421 526 }
bde9f75d
EJ
527 params = json_array_create(json_args, argc);
528 request = jsonrpc_create_request(command, params, NULL);
529
530 error = jsonrpc_transact_block(client, request, &reply);
531 if (error) {
532 VLOG_WARN("error communicating with %s: %s", jsonrpc_get_name(client),
2bf1d3cc 533 ovs_retval_to_string(error));
bde9f75d 534 return error;
064af421
BP
535 }
536
bde9f75d
EJ
537 if (reply->error) {
538 if (reply->error->type == JSON_STRING) {
539 *err = xstrdup(json_string(reply->error));
540 } else {
541 VLOG_WARN("%s: unexpected error type in JSON RPC reply: %s",
542 jsonrpc_get_name(client),
543 json_type_to_string(reply->error->type));
544 error = EINVAL;
064af421 545 }
bde9f75d
EJ
546 } else if (reply->result) {
547 if (reply->result->type == JSON_STRING) {
548 *result = xstrdup(json_string(reply->result));
064af421 549 } else {
bde9f75d
EJ
550 VLOG_WARN("%s: unexpected result type in JSON rpc reply: %s",
551 jsonrpc_get_name(client),
552 json_type_to_string(reply->result->type));
553 error = EINVAL;
064af421
BP
554 }
555 }
064af421 556
bde9f75d
EJ
557 jsonrpc_msg_destroy(reply);
558 return error;
064af421 559}