ovsdb-server: Correct Manager inactivity probe column name.
[cascardo/ovs.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb.h"
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <signal.h>
23 #include <unistd.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "daemon.h"
28 #include "file.h"
29 #include "json.h"
30 #include "jsonrpc.h"
31 #include "jsonrpc-server.h"
32 #include "leak-checker.h"
33 #include "list.h"
34 #include "ovsdb-data.h"
35 #include "ovsdb-types.h"
36 #include "ovsdb-error.h"
37 #include "poll-loop.h"
38 #include "process.h"
39 #include "row.h"
40 #include "stream-ssl.h"
41 #include "stream.h"
42 #include "stress.h"
43 #include "svec.h"
44 #include "table.h"
45 #include "timeval.h"
46 #include "trigger.h"
47 #include "util.h"
48 #include "unixctl.h"
49 #include "vlog.h"
50
51 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
52
53 #if HAVE_OPENSSL
54 /* SSL configuration. */
55 static char *private_key_file;
56 static char *certificate_file;
57 static char *ca_cert_file;
58 static bool bootstrap_ca_cert;
59 #endif
60
61 static unixctl_cb_func ovsdb_server_exit;
62 static unixctl_cb_func ovsdb_server_compact;
63 static unixctl_cb_func ovsdb_server_reconnect;
64
65 static void parse_options(int argc, char *argv[], char **file_namep,
66                           struct shash *remotes, char **unixctl_pathp,
67                           char **run_command);
68 static void usage(void) NO_RETURN;
69
70 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
71                                 const struct ovsdb *db, struct shash *remotes);
72
73 int
74 main(int argc, char *argv[])
75 {
76     char *unixctl_path = NULL;
77     char *run_command = NULL;
78     struct unixctl_server *unixctl;
79     struct ovsdb_jsonrpc_server *jsonrpc;
80     struct shash remotes;
81     struct ovsdb_error *error;
82     struct ovsdb_file *file;
83     struct ovsdb *db;
84     struct process *run_process;
85     char *file_name;
86     bool exiting;
87     int retval;
88
89     proctitle_init(argc, argv);
90     set_program_name(argv[0]);
91     stress_init_command();
92     signal(SIGPIPE, SIG_IGN);
93     process_init();
94
95     parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
96                   &run_command);
97
98     die_if_already_running();
99     daemonize_start();
100
101     error = ovsdb_file_open(file_name, false, &db, &file);
102     if (error) {
103         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
104     }
105
106     jsonrpc = ovsdb_jsonrpc_server_create(db);
107     reconfigure_from_db(jsonrpc, db, &remotes);
108
109     retval = unixctl_server_create(unixctl_path, &unixctl);
110     if (retval) {
111         exit(EXIT_FAILURE);
112     }
113
114     if (run_command) {
115         char *run_argv[4];
116
117         run_argv[0] = "/bin/sh";
118         run_argv[1] = "-c";
119         run_argv[2] = run_command;
120         run_argv[3] = NULL;
121
122         retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
123         if (retval) {
124             ovs_fatal(retval, "%s: process failed to start", run_command);
125         }
126     } else {
127         run_process = NULL;
128     }
129
130     daemonize_complete();
131
132     unixctl_command_register("exit", ovsdb_server_exit, &exiting);
133     unixctl_command_register("ovsdb-server/compact", ovsdb_server_compact,
134                              file);
135     unixctl_command_register("ovsdb-server/reconnect", ovsdb_server_reconnect,
136                              jsonrpc);
137
138     exiting = false;
139     while (!exiting) {
140         reconfigure_from_db(jsonrpc, db, &remotes);
141         ovsdb_jsonrpc_server_run(jsonrpc);
142         unixctl_server_run(unixctl);
143         ovsdb_trigger_run(db, time_msec());
144         if (run_process && process_exited(run_process)) {
145             exiting = true;
146         }
147
148         ovsdb_jsonrpc_server_wait(jsonrpc);
149         unixctl_server_wait(unixctl);
150         ovsdb_trigger_wait(db, time_msec());
151         if (run_process) {
152             process_wait(run_process);
153         }
154         if (exiting) {
155             poll_immediate_wake();
156         }
157         poll_block();
158     }
159     ovsdb_jsonrpc_server_destroy(jsonrpc);
160     ovsdb_destroy(db);
161     shash_destroy(&remotes);
162     unixctl_server_destroy(unixctl);
163
164     if (run_process && process_exited(run_process)) {
165         int status = process_status(run_process);
166         if (status) {
167             ovs_fatal(0, "%s: child exited, %s",
168                       run_command, process_status_msg(status));
169         }
170     }
171
172     return 0;
173 }
174
175 static void
176 parse_db_column(const struct ovsdb *db,
177                 const char *name_,
178                 const struct ovsdb_table **tablep,
179                 const struct ovsdb_column **columnp)
180 {
181     char *name, *table_name, *column_name;
182     const struct ovsdb_column *column;
183     const struct ovsdb_table *table;
184     char *save_ptr = NULL;
185
186     name = xstrdup(name_);
187     strtok_r(name, ":", &save_ptr); /* "db:" */
188     table_name = strtok_r(NULL, ",", &save_ptr);
189     column_name = strtok_r(NULL, ",", &save_ptr);
190     if (!table_name || !column_name) {
191         ovs_fatal(0, "\"%s\": invalid syntax", name_);
192     }
193
194     table = ovsdb_get_table(db, table_name);
195     if (!table) {
196         ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
197     }
198
199     column = ovsdb_table_schema_get_column(table->schema, column_name);
200     if (!column) {
201         ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
202                   name_, table_name, column_name);
203     }
204     free(name);
205
206     *columnp = column;
207     *tablep = table;
208 }
209
210 static void
211 parse_db_string_column(const struct ovsdb *db,
212                        const char *name,
213                        const struct ovsdb_table **tablep,
214                        const struct ovsdb_column **columnp)
215 {
216     const struct ovsdb_column *column;
217     const struct ovsdb_table *table;
218
219     parse_db_column(db, name, &table, &column);
220
221     if (column->type.key.type != OVSDB_TYPE_STRING
222         || column->type.value.type != OVSDB_TYPE_VOID) {
223         ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
224                   "not string or set of strings",
225                   name, table->schema->name, column->name);
226     }
227
228     *columnp = column;
229     *tablep = table;
230 }
231
232 #if HAVE_OPENSSL
233 static const char *
234 query_db_string(const struct ovsdb *db, const char *name)
235 {
236     if (!name || strncmp(name, "db:", 3)) {
237         return name;
238     } else {
239         const struct ovsdb_column *column;
240         const struct ovsdb_table *table;
241         const struct ovsdb_row *row;
242
243         parse_db_string_column(db, name, &table, &column);
244
245         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
246             const struct ovsdb_datum *datum;
247             size_t i;
248
249             datum = &row->fields[column->index];
250             for (i = 0; i < datum->n; i++) {
251                 if (datum->keys[i].string[0]) {
252                     return datum->keys[i].string;
253                 }
254             }
255         }
256         return NULL;
257     }
258 }
259 #endif /* HAVE_OPENSSL */
260
261 static struct ovsdb_jsonrpc_options *
262 add_remote(struct shash *remotes, const char *target)
263 {
264     struct ovsdb_jsonrpc_options *options;
265
266     options = shash_find_data(remotes, target);
267     if (!options) {
268         options = ovsdb_jsonrpc_default_options();
269         shash_add(remotes, target, options);
270     }
271
272     return options;
273 }
274
275 static const union ovsdb_atom *
276 read_column(const struct ovsdb_row *row, const char *column_name,
277             enum ovsdb_atomic_type type)
278 {
279     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
280     const struct ovsdb_table_schema *schema = row->table->schema;
281     const struct ovsdb_column *column;
282     const struct ovsdb_datum *datum;
283
284     column = ovsdb_table_schema_get_column(schema, column_name);
285     if (!column) {
286         VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
287                     schema->name, column_name);
288         return false;
289     }
290
291     if (column->type.key.type != type
292         || column->type.value.type != OVSDB_TYPE_VOID
293         || column->type.n_max != 1) {
294         if (!VLOG_DROP_DBG(&rl)) {
295             char *type_name = ovsdb_type_to_english(&column->type);
296             VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
297                      "type %s.", schema->name, column_name, type_name,
298                      ovsdb_atomic_type_to_string(type));
299         }
300         return false;
301     }
302
303     datum = &row->fields[column->index];
304     return datum->n ? datum->keys : NULL;
305 }
306
307 static bool
308 read_integer_column(const struct ovsdb_row *row, const char *column_name,
309                     long long int *integerp)
310 {
311     const union ovsdb_atom *atom;
312
313     atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
314     *integerp = atom ? atom->integer : 0;
315     return atom != NULL;
316 }
317
318 static bool
319 read_string_column(const struct ovsdb_row *row, const char *column_name,
320                    const char **stringp)
321 {
322     const union ovsdb_atom *atom;
323
324     atom = read_column(row, column_name, OVSDB_TYPE_STRING);
325     *stringp = atom ? atom->string : 0;
326     return atom != NULL;
327 }
328
329 /* Adds a remote and options to 'remotes', based on the Manager table row in
330  * 'row'. */
331 static void
332 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
333 {
334     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
335     struct ovsdb_jsonrpc_options *options;
336     long long int max_backoff, probe_interval;
337     const char *target;
338
339     if (!read_string_column(row, "target", &target) || !target) {
340         VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
341                      row->table->schema->name);
342         return;
343     }
344
345     options = add_remote(remotes, target);
346     if (read_integer_column(row, "max_backoff", &max_backoff)) {
347         options->max_backoff = max_backoff;
348     }
349     if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
350         options->probe_interval = probe_interval;
351     }
352 }
353
354 static void
355 query_db_remotes(const char *name, const struct ovsdb *db,
356                  struct shash *remotes)
357 {
358     const struct ovsdb_column *column;
359     const struct ovsdb_table *table;
360     const struct ovsdb_row *row;
361
362     parse_db_column(db, name, &table, &column);
363
364     if (column->type.key.type == OVSDB_TYPE_STRING
365         && column->type.value.type == OVSDB_TYPE_VOID) {
366         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
367             const struct ovsdb_datum *datum;
368             size_t i;
369
370             datum = &row->fields[column->index];
371             for (i = 0; i < datum->n; i++) {
372                 add_remote(remotes, datum->keys[i].string);
373             }
374         }
375     } else if (column->type.key.type == OVSDB_TYPE_UUID
376                && column->type.key.u.uuid.refTable
377                && column->type.value.type == OVSDB_TYPE_VOID) {
378         const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
379         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
380             const struct ovsdb_datum *datum;
381             size_t i;
382
383             datum = &row->fields[column->index];
384             for (i = 0; i < datum->n; i++) {
385                 const struct ovsdb_row *ref_row;
386
387                 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
388                 if (ref_row) {
389                     add_manager_options(remotes, ref_row);
390                 }
391             }
392         }
393     }
394 }
395
396 /* Reconfigures ovsdb-server based on information in the database. */
397 static void
398 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
399                     const struct ovsdb *db, struct shash *remotes)
400 {
401     struct shash resolved_remotes;
402     struct shash_node *node;
403
404     /* Configure remotes. */
405     shash_init(&resolved_remotes);
406     SHASH_FOR_EACH (node, remotes) {
407         const char *name = node->name;
408
409         if (!strncmp(name, "db:", 3)) {
410             query_db_remotes(name, db, &resolved_remotes);
411         } else {
412             add_remote(&resolved_remotes, name);
413         }
414     }
415     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
416     shash_destroy(&resolved_remotes);
417
418 #if HAVE_OPENSSL
419     /* Configure SSL. */
420     stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
421                                 query_db_string(db, certificate_file));
422     stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
423                                 bootstrap_ca_cert);
424 #endif
425 }
426
427 static void
428 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
429                   void *exiting_)
430 {
431     bool *exiting = exiting_;
432     *exiting = true;
433     unixctl_command_reply(conn, 200, NULL);
434 }
435
436 static void
437 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
438                      void *file_)
439 {
440     struct ovsdb_file *file = file_;
441     struct ovsdb_error *error;
442
443     VLOG_INFO("compacting database by user request");
444     error = ovsdb_file_compact(file);
445     if (!error) {
446         unixctl_command_reply(conn, 200, NULL);
447     } else {
448         char *s = ovsdb_error_to_string(error);
449         ovsdb_error_destroy(error);
450         unixctl_command_reply(conn, 503, s);
451         free(s);
452     }
453 }
454
455 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
456  * connections and reconnect. */
457 static void
458 ovsdb_server_reconnect(struct unixctl_conn *conn, const char *args OVS_UNUSED,
459                        void *jsonrpc_)
460 {
461     struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
462
463     ovsdb_jsonrpc_server_reconnect(jsonrpc);
464     unixctl_command_reply(conn, 200, NULL);
465 }
466
467 static void
468 parse_options(int argc, char *argv[], char **file_namep,
469               struct shash *remotes, char **unixctl_pathp,
470               char **run_command)
471 {
472     enum {
473         OPT_DUMMY = UCHAR_MAX + 1,
474         OPT_REMOTE,
475         OPT_UNIXCTL,
476         OPT_RUN,
477         OPT_BOOTSTRAP_CA_CERT,
478         VLOG_OPTION_ENUMS,
479         LEAK_CHECKER_OPTION_ENUMS
480     };
481     static struct option long_options[] = {
482         {"remote",      required_argument, 0, OPT_REMOTE},
483         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
484         {"run",         required_argument, 0, OPT_RUN},
485         {"help",        no_argument, 0, 'h'},
486         {"version",     no_argument, 0, 'V'},
487         DAEMON_LONG_OPTIONS,
488         VLOG_LONG_OPTIONS,
489         LEAK_CHECKER_LONG_OPTIONS,
490 #ifdef HAVE_OPENSSL
491         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
492         {"private-key", required_argument, 0, 'p'},
493         {"certificate", required_argument, 0, 'c'},
494         {"ca-cert",     required_argument, 0, 'C'},
495 #endif
496         {0, 0, 0, 0},
497     };
498     char *short_options = long_options_to_short_options(long_options);
499
500     shash_init(remotes);
501     for (;;) {
502         int c;
503
504         c = getopt_long(argc, argv, short_options, long_options, NULL);
505         if (c == -1) {
506             break;
507         }
508
509         switch (c) {
510         case OPT_REMOTE:
511             shash_add_once(remotes, optarg, NULL);
512             break;
513
514         case OPT_UNIXCTL:
515             *unixctl_pathp = optarg;
516             break;
517
518         case OPT_RUN:
519             *run_command = optarg;
520             break;
521
522         case 'h':
523             usage();
524
525         case 'V':
526             OVS_PRINT_VERSION(0, 0);
527             exit(EXIT_SUCCESS);
528
529         VLOG_OPTION_HANDLERS
530         DAEMON_OPTION_HANDLERS
531         LEAK_CHECKER_OPTION_HANDLERS
532
533 #ifdef HAVE_OPENSSL
534         case 'p':
535             private_key_file = optarg;
536             break;
537
538         case 'c':
539             certificate_file = optarg;
540             break;
541
542         case 'C':
543             ca_cert_file = optarg;
544             bootstrap_ca_cert = false;
545             break;
546
547         case OPT_BOOTSTRAP_CA_CERT:
548             ca_cert_file = optarg;
549             bootstrap_ca_cert = true;
550             break;
551 #endif
552
553         case '?':
554             exit(EXIT_FAILURE);
555
556         default:
557             abort();
558         }
559     }
560     free(short_options);
561
562     argc -= optind;
563     argv += optind;
564
565     if (argc > 1) {
566         ovs_fatal(0, "database file is only non-option argument; "
567                 "use --help for usage");
568     } else if (argc < 1) {
569         ovs_fatal(0, "missing database file argument; use --help for usage");
570     }
571
572     *file_namep = argv[0];
573 }
574
575 static void
576 usage(void)
577 {
578     printf("%s: Open vSwitch database server\n"
579            "usage: %s [OPTIONS] DATABASE\n"
580            "where DATABASE is a database file in ovsdb format.\n",
581            program_name, program_name);
582     printf("\nJSON-RPC options (may be specified any number of times):\n"
583            "  --remote=REMOTE         connect or listen to REMOTE\n");
584     stream_usage("JSON-RPC", true, true, true);
585     daemon_usage();
586     vlog_usage();
587     printf("\nOther options:\n"
588            "  --run COMMAND           run COMMAND as subprocess then exit\n"
589            "  --unixctl=SOCKET        override default control socket name\n"
590            "  -h, --help              display this help message\n"
591            "  -V, --version           display version information\n");
592     leak_checker_usage();
593     exit(EXIT_SUCCESS);
594 }