]> git.proxmox.com Git - ovs.git/blame - lib/unixctl.c
flow: Enable retrieval of TCP flags from IPv6 traffic.
[ovs.git] / lib / unixctl.c
CommitLineData
064af421 1/*
bde9f75d 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
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"
19#include <assert.h>
064af421 20#include <errno.h>
064af421
BP
21#include <unistd.h>
22#include "coverage.h"
23#include "dirs.h"
24#include "dynamic-string.h"
bde9f75d
EJ
25#include "json.h"
26#include "jsonrpc.h"
064af421 27#include "list.h"
064af421
BP
28#include "poll-loop.h"
29#include "shash.h"
bde9f75d 30#include "stream.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 *
98 * 'cb' is called when the command is received. It is passed the actual set of
99 * arguments, as a text string, plus a copy of 'aux'. Normally 'cb' should
bde9f75d
EJ
100 * reply by calling unixctl_command_reply() or unixctl_command_reply_error()
101 * before it returns, but if the command cannot be handled immediately then it
102 * can defer the reply until later. A given connection can only process a
103 * single request at a time, so a reply must be made eventually to avoid
104 * 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
113 assert(!lookup || lookup->cb == cb);
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);
136 assert(conn->request_id);
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
EJ
171/* Replies to the active unixctl connection 'conn'. 'error' is sent to the
172 * client indicating an error occured processing the command. Only one call to
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
181/* Creates a unixctl server listening on 'path', which may be:
182 *
183 * - NULL, in which case <rundir>/<program>.<pid>.ctl is used.
184 *
614c4892
BP
185 * - "none", in which case the function will return successfully but
186 * no socket will actually be created.
187 *
064af421
BP
188 * - A name that does not start with '/', in which case it is put in
189 * <rundir>.
190 *
191 * - An absolute path (starting with '/') that gives the exact name of
192 * the Unix domain socket to listen on.
193 *
194 * A program that (optionally) daemonizes itself should call this function
195 * *after* daemonization, so that the socket name contains the pid of the
196 * daemon instead of the pid of the program that exited. (Otherwise,
3fbe1d30 197 * "ovs-appctl --target=<program>" will fail.)
064af421
BP
198 *
199 * Returns 0 if successful, otherwise a positive errno value. If successful,
614c4892
BP
200 * sets '*serverp' to the new unixctl_server (or to NULL if 'path' was "none"),
201 * otherwise to NULL. */
064af421
BP
202int
203unixctl_server_create(const char *path, struct unixctl_server **serverp)
204{
205 struct unixctl_server *server;
bde9f75d
EJ
206 struct pstream *listener;
207 char *punix_path;
064af421
BP
208 int error;
209
bde9f75d 210 *serverp = NULL;
614c4892 211 if (path && !strcmp(path, "none")) {
614c4892
BP
212 return 0;
213 }
214
064af421 215 if (path) {
bde9f75d
EJ
216 char *abs_path = abs_file_name(ovs_rundir(), path);
217 punix_path = xasprintf("punix:%s", abs_path);
218 free(abs_path);
064af421 219 } else {
bde9f75d
EJ
220 punix_path = xasprintf("punix:%s/%s.%ld.ctl", ovs_rundir(),
221 program_name, (long int) getpid());
064af421
BP
222 }
223
bde9f75d 224 error = pstream_open(punix_path, &listener);
bde9f75d 225 if (error) {
324f0c59
BP
226 ovs_error(error, "could not initialize control socket %s", punix_path);
227 goto exit;
064af421
BP
228 }
229
bde9f75d
EJ
230 unixctl_command_register("help", "", 0, 0, unixctl_help, NULL);
231 unixctl_command_register("version", "", 0, 0, unixctl_version, NULL);
064af421 232
bde9f75d
EJ
233 server = xmalloc(sizeof *server);
234 server->listener = listener;
235 list_init(&server->conns);
064af421 236 *serverp = server;
324f0c59
BP
237
238exit:
239 free(punix_path);
240 return error;
064af421
BP
241}
242
243static void
bde9f75d 244process_command(struct unixctl_conn *conn, struct jsonrpc_msg *request)
064af421 245{
bde9f75d 246 char *error = NULL;
064af421 247
064af421 248 struct unixctl_command *command;
bde9f75d 249 struct json_array *params;
064af421
BP
250
251 COVERAGE_INC(unixctl_received);
bde9f75d
EJ
252 conn->request_id = json_clone(request->id);
253
254 params = json_array(request->params);
255 command = shash_find_data(&commands, request->method);
256 if (!command) {
257 error = xasprintf("\"%s\" is not a valid command", request->method);
258 } else if (params->n < command->min_args) {
259 error = xasprintf("\"%s\" command requires at least %d arguments",
260 request->method, command->min_args);
261 } else if (params->n > command->max_args) {
262 error = xasprintf("\"%s\" command takes at most %d arguments",
263 request->method, command->max_args);
064af421 264 } else {
bde9f75d
EJ
265 struct svec argv = SVEC_EMPTY_INITIALIZER;
266 int i;
267
268 svec_add(&argv, request->method);
269 for (i = 0; i < params->n; i++) {
270 if (params->elems[i]->type != JSON_STRING) {
271 error = xasprintf("\"%s\" command has non-string argument",
272 request->method);
273 break;
274 }
275 svec_add(&argv, json_string(params->elems[i]));
276 }
277 svec_terminate(&argv);
278
279 if (!error) {
0e15264f
BP
280 command->cb(conn, argv.n, (const char **) argv.names,
281 command->aux);
282 }
283
bde9f75d 284 svec_destroy(&argv);
064af421 285 }
0e15264f 286
bde9f75d
EJ
287 if (error) {
288 unixctl_command_reply_error(conn, error);
289 free(error);
290 }
064af421
BP
291}
292
293static int
bde9f75d 294run_connection(struct unixctl_conn *conn)
064af421 295{
bde9f75d 296 int error, i;
064af421 297
bde9f75d
EJ
298 jsonrpc_run(conn->rpc);
299 error = jsonrpc_get_status(conn->rpc);
300 if (error || jsonrpc_get_backlog(conn->rpc)) {
301 return error;
302 }
064af421 303
bde9f75d
EJ
304 for (i = 0; i < 10; i++) {
305 struct jsonrpc_msg *msg;
064af421 306
bde9f75d
EJ
307 if (error || conn->request_id) {
308 break;
064af421
BP
309 }
310
bde9f75d
EJ
311 jsonrpc_recv(conn->rpc, &msg);
312 if (msg) {
313 if (msg->type == JSONRPC_REQUEST) {
314 process_command(conn, msg);
064af421 315 } else {
bde9f75d
EJ
316 VLOG_WARN_RL(&rl, "%s: received unexpected %s message",
317 jsonrpc_get_name(conn->rpc),
318 jsonrpc_msg_type_to_string(msg->type));
319 error = EINVAL;
064af421 320 }
bde9f75d 321 jsonrpc_msg_destroy(msg);
064af421 322 }
bde9f75d 323 error = error ? error : jsonrpc_get_status(conn->rpc);
064af421 324 }
064af421 325
bde9f75d 326 return error;
064af421
BP
327}
328
329static void
330kill_connection(struct unixctl_conn *conn)
331{
332 list_remove(&conn->node);
bde9f75d
EJ
333 jsonrpc_close(conn->rpc);
334 json_destroy(conn->request_id);
064af421
BP
335 free(conn);
336}
337
338void
339unixctl_server_run(struct unixctl_server *server)
340{
341 struct unixctl_conn *conn, *next;
342 int i;
343
614c4892
BP
344 if (!server) {
345 return;
346 }
347
064af421 348 for (i = 0; i < 10; i++) {
bde9f75d
EJ
349 struct stream *stream;
350 int error;
351
352 error = pstream_accept(server->listener, &stream);
353 if (!error) {
354 struct unixctl_conn *conn = xzalloc(sizeof *conn);
355 list_push_back(&server->conns, &conn->node);
356 conn->rpc = jsonrpc_open(stream);
357 } else if (error == EAGAIN) {
064af421 358 break;
bde9f75d
EJ
359 } else {
360 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
361 pstream_get_name(server->listener),
362 strerror(error));
064af421 363 }
064af421
BP
364 }
365
4e8e4213 366 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
064af421
BP
367 int error = run_connection(conn);
368 if (error && error != EAGAIN) {
369 kill_connection(conn);
370 }
371 }
372}
373
374void
375unixctl_server_wait(struct unixctl_server *server)
376{
377 struct unixctl_conn *conn;
378
614c4892
BP
379 if (!server) {
380 return;
381 }
382
bde9f75d 383 pstream_wait(server->listener);
4e8e4213 384 LIST_FOR_EACH (conn, node, &server->conns) {
bde9f75d
EJ
385 jsonrpc_wait(conn->rpc);
386 if (!jsonrpc_get_backlog(conn->rpc)) {
387 jsonrpc_recv_wait(conn->rpc);
064af421
BP
388 }
389 }
390}
391
392/* Destroys 'server' and stops listening for connections. */
393void
394unixctl_server_destroy(struct unixctl_server *server)
395{
396 if (server) {
397 struct unixctl_conn *conn, *next;
398
4e8e4213 399 LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) {
064af421
BP
400 kill_connection(conn);
401 }
402
bde9f75d 403 pstream_close(server->listener);
064af421
BP
404 free(server);
405 }
406}
407\f
bde9f75d
EJ
408/* Connects to a unixctl server socket. 'path' should be the name of a unixctl
409 * server socket. If it does not start with '/', it will be prefixed with the
410 * rundir (e.g. /usr/local/var/run/openvswitch).
064af421
BP
411 *
412 * Returns 0 if successful, otherwise a positive errno value. If successful,
bde9f75d 413 * sets '*client' to the new jsonrpc, otherwise to NULL. */
064af421 414int
bde9f75d 415unixctl_client_create(const char *path, struct jsonrpc **client)
064af421 416{
bde9f75d
EJ
417 char *abs_path, *unix_path;
418 struct stream *stream;
064af421 419 int error;
064af421 420
bde9f75d 421 *client = NULL;
064af421 422
bde9f75d
EJ
423 abs_path = abs_file_name(ovs_rundir(), path);
424 unix_path = xasprintf("unix:%s", abs_path);
425 error = stream_open_block(stream_open(unix_path, &stream), &stream);
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}