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