]> git.proxmox.com Git - mirror_ovs.git/blame - lib/unixctl.c
ovsdb-idl: Fix iteration over tracked rows with no actual data.
[mirror_ovs.git] / lib / unixctl.c
CommitLineData
064af421 1/*
bb338451 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 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"
3e8a2ad1 23#include "openvswitch/dynamic-string.h"
ee89ea7b 24#include "openvswitch/json.h"
bde9f75d 25#include "jsonrpc.h"
b19bab5b 26#include "openvswitch/list.h"
fd016ae3 27#include "openvswitch/poll-loop.h"
ee89ea7b 28#include "openvswitch/shash.h"
bde9f75d 29#include "stream.h"
cb54a8c5 30#include "stream-provider.h"
3c442619 31#include "svec.h"
e6211adc 32#include "openvswitch/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 {
ca6ba700 47 struct ovs_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;
ca6ba700 58 struct ovs_list conns;
295fc4d6 59 char *path;
064af421
BP
60};
61
064af421
BP
62static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
63
64static struct shash commands = SHASH_INITIALIZER(&commands);
65
66static void
91a11f5b
AW
67unixctl_list_commands(struct unixctl_conn *conn, int argc OVS_UNUSED,
68 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
064af421
BP
69{
70 struct ds ds = DS_EMPTY_INITIALIZER;
7ff2009a 71 const struct shash_node **nodes = shash_sort(&commands);
3c442619 72 size_t i;
064af421
BP
73
74 ds_put_cstr(&ds, "The available commands are:\n");
3c442619 75
7ff2009a
JP
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;
bde9f75d 79
91fc374a
BP
80 if (command->usage) {
81 ds_put_format(&ds, " %-23s %s\n", node->name, command->usage);
82 }
3c442619 83 }
7ff2009a 84 free(nodes);
3c442619 85
bde9f75d 86 unixctl_command_reply(conn, ds_cstr(&ds));
064af421
BP
87 ds_destroy(&ds);
88}
89
d5e1e5ed 90static void
0e15264f
BP
91unixctl_version(struct unixctl_conn *conn, int argc OVS_UNUSED,
92 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
d5e1e5ed 93{
b0248b2a 94 unixctl_command_reply(conn, ovs_get_program_version());
d5e1e5ed
JP
95}
96
0e15264f
BP
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
91fc374a 99 * "list-commands" output. (If 'usage' is NULL, then the command is hidden.)
0e15264f 100 *
b558fd90
BP
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. */
064af421 108void
0e15264f
BP
109unixctl_command_register(const char *name, const char *usage,
110 int min_args, int max_args,
111 unixctl_cb_func *cb, void *aux)
064af421
BP
112{
113 struct unixctl_command *command;
4895c701
SH
114 struct unixctl_command *lookup = shash_find_data(&commands, name);
115
cb22974d 116 ovs_assert(!lookup || lookup->cb == cb);
4895c701
SH
117
118 if (lookup) {
119 return;
120 }
064af421 121
064af421 122 command = xmalloc(sizeof *command);
0e15264f
BP
123 command->usage = usage;
124 command->min_args = min_args;
125 command->max_args = max_args;
064af421 126 command->cb = cb;
8ca79daa 127 command->aux = aux;
064af421
BP
128 shash_add(&commands, name, command);
129}
130
bde9f75d
EJ
131static void
132unixctl_command_reply__(struct unixctl_conn *conn,
133 bool success, const char *body)
064af421 134{
bde9f75d
EJ
135 struct json *body_json;
136 struct jsonrpc_msg *reply;
137
138 COVERAGE_INC(unixctl_replied);
cb22974d 139 ovs_assert(conn->request_id);
bde9f75d
EJ
140
141 if (!body) {
142 body = "";
064af421 143 }
bde9f75d
EJ
144
145 if (body[0] && body[strlen(body) - 1] != '\n') {
146 body_json = json_string_create_nocopy(xasprintf("%s\n", body));
147 } else {
148 body_json = json_string_create(body);
149 }
150
151 if (success) {
152 reply = jsonrpc_create_reply(body_json, conn->request_id);
153 } else {
154 reply = jsonrpc_create_error(body_json, conn->request_id);
155 }
156
bb338451
BP
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);
161 free(id);
162 }
163
bde9f75d
EJ
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;
064af421
BP
169}
170
bde9f75d
EJ
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
174 * request. */
064af421 175void
bde9f75d 176unixctl_command_reply(struct unixctl_conn *conn, const char *result)
064af421 177{
bde9f75d
EJ
178 unixctl_command_reply__(conn, true, result);
179}
064af421 180
bde9f75d 181/* Replies to the active unixctl connection 'conn'. 'error' is sent to the
ec9f40dc 182 * client indicating an error occurred processing the command. Only one call to
bde9f75d
EJ
183 * unixctl_command_reply() or unixctl_command_reply_error() may be made per
184 * request. */
185void
186unixctl_command_reply_error(struct unixctl_conn *conn, const char *error)
187{
188 unixctl_command_reply__(conn, false, error);
064af421
BP
189}
190
cb54a8c5 191/* Creates a unixctl server listening on 'path', which for POSIX may be:
064af421
BP
192 *
193 * - NULL, in which case <rundir>/<program>.<pid>.ctl is used.
194 *
195 * - A name that does not start with '/', in which case it is put in
196 * <rundir>.
197 *
198 * - An absolute path (starting with '/') that gives the exact name of
199 * the Unix domain socket to listen on.
200 *
922247c6 201 * For Windows, a local named pipe is used. A file is created in 'path'
cb54a8c5
GS
202 * which may be:
203 *
204 * - NULL, in which case <rundir>/<program>.ctl is used.
205 *
206 * - An absolute path that gives the name of the file.
207 *
208 * For both POSIX and Windows, if the path is "none", the function will
209 * return successfully but no socket will actually be created.
210 *
064af421
BP
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,
3fbe1d30 214 * "ovs-appctl --target=<program>" will fail.)
064af421
BP
215 *
216 * Returns 0 if successful, otherwise a positive errno value. If successful,
614c4892
BP
217 * sets '*serverp' to the new unixctl_server (or to NULL if 'path' was "none"),
218 * otherwise to NULL. */
064af421
BP
219int
220unixctl_server_create(const char *path, struct unixctl_server **serverp)
221{
bde9f75d 222 *serverp = NULL;
614c4892 223 if (path && !strcmp(path, "none")) {
614c4892
BP
224 return 0;
225 }
226
295fc4d6
BP
227#ifdef _WIN32
228 enum { WINDOWS = 1 };
cb54a8c5 229#else
295fc4d6 230 enum { WINDOWS = 0 };
cb54a8c5 231#endif
064af421 232
295fc4d6
BP
233 long int pid = getpid();
234 char *abs_path
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));
238
239 struct pstream *listener;
240 char *punix_path = xasprintf("punix:%s", abs_path);
241 int error = pstream_open(punix_path, &listener, 0);
242 free(punix_path);
243
bde9f75d 244 if (error) {
295fc4d6
BP
245 ovs_error(error, "%s: could not initialize control socket", abs_path);
246 free(abs_path);
247 return error;
064af421
BP
248 }
249
91a11f5b
AW
250 unixctl_command_register("list-commands", "", 0, 0, unixctl_list_commands,
251 NULL);
bde9f75d 252 unixctl_command_register("version", "", 0, 0, unixctl_version, NULL);
064af421 253
295fc4d6 254 struct unixctl_server *server = xmalloc(sizeof *server);
bde9f75d 255 server->listener = listener;
295fc4d6 256 server->path = abs_path;
417e7e66 257 ovs_list_init(&server->conns);
064af421 258 *serverp = server;
295fc4d6 259 return 0;
064af421
BP
260}
261
262static void
bde9f75d 263process_command(struct unixctl_conn *conn, struct jsonrpc_msg *request)
064af421 264{
bde9f75d 265 char *error = NULL;
064af421 266
064af421 267 struct unixctl_command *command;
bde9f75d 268 struct json_array *params;
064af421
BP
269
270 COVERAGE_INC(unixctl_received);
bde9f75d
EJ
271 conn->request_id = json_clone(request->id);
272
bb338451
BP
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);
278 free(params_s);
279 free(id_s);
280 }
281
bde9f75d
EJ
282 params = json_array(request->params);
283 command = shash_find_data(&commands, request->method);
284 if (!command) {
4410f206
BP
285 error = xasprintf("\"%s\" is not a valid command (use "
286 "\"list-commands\" to see a list of valid commands)",
287 request->method);
bde9f75d
EJ
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);
064af421 294 } else {
bde9f75d
EJ
295 struct svec argv = SVEC_EMPTY_INITIALIZER;
296 int i;
297
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",
302 request->method);
303 break;
304 }
305 svec_add(&argv, json_string(params->elems[i]));
306 }
307 svec_terminate(&argv);
308
309 if (!error) {
0e15264f
BP
310 command->cb(conn, argv.n, (const char **) argv.names,
311 command->aux);
312 }
313
bde9f75d 314 svec_destroy(&argv);
064af421 315 }
0e15264f 316
bde9f75d
EJ
317 if (error) {
318 unixctl_command_reply_error(conn, error);
319 free(error);
320 }
064af421
BP
321}
322
323static int
bde9f75d 324run_connection(struct unixctl_conn *conn)
064af421 325{
bde9f75d 326 int error, i;
064af421 327
bde9f75d
EJ
328 jsonrpc_run(conn->rpc);
329 error = jsonrpc_get_status(conn->rpc);
330 if (error || jsonrpc_get_backlog(conn->rpc)) {
331 return error;
332 }
064af421 333
bde9f75d
EJ
334 for (i = 0; i < 10; i++) {
335 struct jsonrpc_msg *msg;
064af421 336
bde9f75d
EJ
337 if (error || conn->request_id) {
338 break;
064af421
BP
339 }
340
bde9f75d
EJ
341 jsonrpc_recv(conn->rpc, &msg);
342 if (msg) {
343 if (msg->type == JSONRPC_REQUEST) {
344 process_command(conn, msg);
064af421 345 } else {
bde9f75d
EJ
346 VLOG_WARN_RL(&rl, "%s: received unexpected %s message",
347 jsonrpc_get_name(conn->rpc),
348 jsonrpc_msg_type_to_string(msg->type));
349 error = EINVAL;
064af421 350 }
bde9f75d 351 jsonrpc_msg_destroy(msg);
064af421 352 }
bde9f75d 353 error = error ? error : jsonrpc_get_status(conn->rpc);
064af421 354 }
064af421 355
bde9f75d 356 return error;
064af421
BP
357}
358
359static void
360kill_connection(struct unixctl_conn *conn)
361{
417e7e66 362 ovs_list_remove(&conn->node);
bde9f75d
EJ
363 jsonrpc_close(conn->rpc);
364 json_destroy(conn->request_id);
064af421
BP
365 free(conn);
366}
367
368void
369unixctl_server_run(struct unixctl_server *server)
370{
614c4892
BP
371 if (!server) {
372 return;
373 }
374
71f21279 375 for (int i = 0; i < 10; i++) {
bde9f75d
EJ
376 struct stream *stream;
377 int error;
378
379 error = pstream_accept(server->listener, &stream);
380 if (!error) {
381 struct unixctl_conn *conn = xzalloc(sizeof *conn);
417e7e66 382 ovs_list_push_back(&server->conns, &conn->node);
bde9f75d
EJ
383 conn->rpc = jsonrpc_open(stream);
384 } else if (error == EAGAIN) {
064af421 385 break;
bde9f75d
EJ
386 } else {
387 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
388 pstream_get_name(server->listener),
10a89ef0 389 ovs_strerror(error));
064af421 390 }
064af421
BP
391 }
392
71f21279 393 struct unixctl_conn *conn, *next;
4e8e4213 394 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
064af421
BP
395 int error = run_connection(conn);
396 if (error && error != EAGAIN) {
397 kill_connection(conn);
398 }
399 }
400}
401
402void
403unixctl_server_wait(struct unixctl_server *server)
404{
405 struct unixctl_conn *conn;
406
614c4892
BP
407 if (!server) {
408 return;
409 }
410
bde9f75d 411 pstream_wait(server->listener);
4e8e4213 412 LIST_FOR_EACH (conn, node, &server->conns) {
bde9f75d 413 jsonrpc_wait(conn->rpc);
21b52216 414 if (!jsonrpc_get_backlog(conn->rpc) && !conn->request_id) {
bde9f75d 415 jsonrpc_recv_wait(conn->rpc);
064af421
BP
416 }
417 }
418}
419
420/* Destroys 'server' and stops listening for connections. */
421void
422unixctl_server_destroy(struct unixctl_server *server)
423{
424 if (server) {
425 struct unixctl_conn *conn, *next;
426
4e8e4213 427 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
064af421
BP
428 kill_connection(conn);
429 }
430
187f7d60 431 free(server->path);
bde9f75d 432 pstream_close(server->listener);
064af421
BP
433 free(server);
434 }
435}
295fc4d6
BP
436
437const char *
438unixctl_server_get_path(const struct unixctl_server *server)
439{
440 return server ? server->path : NULL;
441}
064af421 442\f
cb54a8c5
GS
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).
446 *
922247c6
AS
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.
cb54a8c5 449 * 'path' should be an absolute path of the file.
064af421
BP
450 *
451 * Returns 0 if successful, otherwise a positive errno value. If successful,
bde9f75d 452 * sets '*client' to the new jsonrpc, otherwise to NULL. */
064af421 453int
bde9f75d 454unixctl_client_create(const char *path, struct jsonrpc **client)
064af421 455{
bde9f75d 456 struct stream *stream;
064af421 457 int error;
cb54a8c5 458
4c36252d
BP
459 char *abs_path = abs_file_name(ovs_rundir(), path);
460 char *unix_path = xasprintf("unix:%s", abs_path);
cb54a8c5
GS
461
462 *client = NULL;
463
f125905c 464 error = stream_open_block(stream_open(unix_path, &stream, DSCP_DEFAULT),
77f42ca5 465 -1, &stream);
bde9f75d
EJ
466 free(unix_path);
467 free(abs_path);
064af421 468
bde9f75d
EJ
469 if (error) {
470 VLOG_WARN("failed to connect to %s", path);
471 return error;
064af421 472 }
bde9f75d
EJ
473
474 *client = jsonrpc_open(stream);
475 return 0;
064af421
BP
476}
477
bde9f75d
EJ
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. */
064af421 484int
bde9f75d
EJ
485unixctl_client_transact(struct jsonrpc *client, const char *command, int argc,
486 char *argv[], char **result, char **err)
064af421 487{
bde9f75d
EJ
488 struct jsonrpc_msg *request, *reply;
489 struct json **json_args, *params;
490 int error, i;
064af421 491
bde9f75d
EJ
492 *result = NULL;
493 *err = NULL;
494
495 json_args = xmalloc(argc * sizeof *json_args);
496 for (i = 0; i < argc; i++) {
497 json_args[i] = json_string_create(argv[i]);
064af421 498 }
bde9f75d
EJ
499 params = json_array_create(json_args, argc);
500 request = jsonrpc_create_request(command, params, NULL);
501
502 error = jsonrpc_transact_block(client, request, &reply);
503 if (error) {
504 VLOG_WARN("error communicating with %s: %s", jsonrpc_get_name(client),
2bf1d3cc 505 ovs_retval_to_string(error));
bde9f75d 506 return error;
064af421
BP
507 }
508
bde9f75d
EJ
509 if (reply->error) {
510 if (reply->error->type == JSON_STRING) {
511 *err = xstrdup(json_string(reply->error));
512 } else {
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));
516 error = EINVAL;
064af421 517 }
bde9f75d
EJ
518 } else if (reply->result) {
519 if (reply->result->type == JSON_STRING) {
520 *result = xstrdup(json_string(reply->result));
064af421 521 } else {
bde9f75d
EJ
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));
525 error = EINVAL;
064af421
BP
526 }
527 }
064af421 528
bde9f75d
EJ
529 jsonrpc_msg_destroy(reply);
530 return error;
064af421 531}