6032d732067bc9fc70466052e605a02eb4aaba04
[sliver-openvswitch.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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 <assert.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <signal.h>
22 #include <unistd.h>
23
24 #include "column.h"
25 #include "command-line.h"
26 #include "daemon.h"
27 #include "dirs.h"
28 #include "dummy.h"
29 #include "dynamic-string.h"
30 #include "file.h"
31 #include "hash.h"
32 #include "json.h"
33 #include "jsonrpc.h"
34 #include "jsonrpc-server.h"
35 #include "leak-checker.h"
36 #include "list.h"
37 #include "memory.h"
38 #include "ovsdb.h"
39 #include "ovsdb-data.h"
40 #include "ovsdb-types.h"
41 #include "ovsdb-error.h"
42 #include "poll-loop.h"
43 #include "process.h"
44 #include "row.h"
45 #include "simap.h"
46 #include "stream-ssl.h"
47 #include "stream.h"
48 #include "stress.h"
49 #include "sset.h"
50 #include "table.h"
51 #include "timeval.h"
52 #include "transaction.h"
53 #include "trigger.h"
54 #include "util.h"
55 #include "unixctl.h"
56 #include "vlog.h"
57
58 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
59
60 struct db {
61     /* Initialized in main(). */
62     char *filename;
63     struct ovsdb_file *file;
64     struct ovsdb *db;
65
66     /* Only used by update_remote_status(). */
67     struct ovsdb_txn *txn;
68 };
69
70 /* SSL configuration. */
71 static char *private_key_file;
72 static char *certificate_file;
73 static char *ca_cert_file;
74 static bool bootstrap_ca_cert;
75
76 static unixctl_cb_func ovsdb_server_exit;
77 static unixctl_cb_func ovsdb_server_compact;
78 static unixctl_cb_func ovsdb_server_reconnect;
79
80 static void parse_options(int *argc, char **argvp[],
81                           struct sset *remotes, char **unixctl_pathp,
82                           char **run_command);
83 static void usage(void) NO_RETURN;
84
85 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
86                                 const struct db dbs[], size_t n_dbs,
87                                 struct sset *remotes);
88
89 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
90                                  const struct sset *remotes,
91                                  struct db dbs[], size_t n_dbs);
92
93 int
94 main(int argc, char *argv[])
95 {
96     char *unixctl_path = NULL;
97     char *run_command = NULL;
98     struct unixctl_server *unixctl;
99     struct ovsdb_jsonrpc_server *jsonrpc;
100     struct sset remotes;
101     struct process *run_process;
102     bool exiting;
103     int retval;
104     long long int status_timer = LLONG_MIN;
105
106     struct db *dbs;
107     int n_dbs;
108     int i;
109
110     proctitle_init(argc, argv);
111     set_program_name(argv[0]);
112     stress_init_command();
113     signal(SIGPIPE, SIG_IGN);
114     process_init();
115
116     parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
117
118     daemonize_start();
119
120     n_dbs = MAX(1, argc);
121     dbs = xcalloc(n_dbs + 1, sizeof *dbs);
122     if (argc > 0) {
123         for (i = 0; i < argc; i++) {
124             dbs[i].filename = argv[i];
125         }
126     } else {
127         dbs[0].filename = xasprintf("%s/conf.db", ovs_dbdir());
128     }
129
130     for (i = 0; i < n_dbs; i++) {
131         struct ovsdb_error *error;
132
133         error = ovsdb_file_open(dbs[i].filename, false,
134                                 &dbs[i].db, &dbs[i].file);
135         if (error) {
136             ovs_fatal(0, "%s", ovsdb_error_to_string(error));
137         }
138     }
139
140     jsonrpc = ovsdb_jsonrpc_server_create();
141     for (i = 0; i < n_dbs; i++) {
142         if (!ovsdb_jsonrpc_server_add_db(jsonrpc, dbs[i].db)) {
143             ovs_fatal(0, "%s: duplicate database name",
144                       dbs[i].db->schema->name);
145         }
146     }
147     reconfigure_from_db(jsonrpc, dbs, n_dbs, &remotes);
148
149     retval = unixctl_server_create(unixctl_path, &unixctl);
150     if (retval) {
151         exit(EXIT_FAILURE);
152     }
153
154     if (run_command) {
155         char *run_argv[4];
156
157         run_argv[0] = "/bin/sh";
158         run_argv[1] = "-c";
159         run_argv[2] = run_command;
160         run_argv[3] = NULL;
161
162         retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
163         if (retval) {
164             ovs_fatal(retval, "%s: process failed to start", run_command);
165         }
166     } else {
167         run_process = NULL;
168     }
169
170     daemonize_complete();
171
172     if (!run_command) {
173         /* ovsdb-server is usually a long-running process, in which case it
174          * makes plenty of sense to log the version, but --run makes
175          * ovsdb-server more like a command-line tool, so skip it.  */
176         VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
177     }
178
179     unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
180     unixctl_command_register("ovsdb-server/compact", "", 0, 1,
181                              ovsdb_server_compact, dbs);
182     unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
183                              ovsdb_server_reconnect, jsonrpc);
184
185     exiting = false;
186     while (!exiting) {
187         int i;
188
189         memory_run();
190         if (memory_should_report()) {
191             struct simap usage;
192
193             simap_init(&usage);
194             ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
195             for (i = 0; i < n_dbs; i++) {
196                 ovsdb_get_memory_usage(dbs[i].db, &usage);
197             }
198             memory_report(&usage);
199             simap_destroy(&usage);
200         }
201
202         reconfigure_from_db(jsonrpc, dbs, n_dbs, &remotes);
203         ovsdb_jsonrpc_server_run(jsonrpc);
204         unixctl_server_run(unixctl);
205
206         for (i = 0; i < n_dbs; i++) {
207             ovsdb_trigger_run(dbs[i].db, time_msec());
208         }
209         if (run_process && process_exited(run_process)) {
210             exiting = true;
211         }
212
213         /* update Manager status(es) every 5 seconds */
214         if (time_msec() >= status_timer) {
215             status_timer = time_msec() + 5000;
216             update_remote_status(jsonrpc, &remotes, dbs, n_dbs);
217         }
218
219         memory_wait();
220         ovsdb_jsonrpc_server_wait(jsonrpc);
221         unixctl_server_wait(unixctl);
222         for (i = 0; i < n_dbs; i++) {
223             ovsdb_trigger_wait(dbs[i].db, time_msec());
224         }
225         if (run_process) {
226             process_wait(run_process);
227         }
228         if (exiting) {
229             poll_immediate_wake();
230         }
231         poll_timer_wait_until(status_timer);
232         poll_block();
233     }
234     ovsdb_jsonrpc_server_destroy(jsonrpc);
235     for (i = 0; i < n_dbs; i++) {
236         ovsdb_destroy(dbs[i].db);
237     }
238     sset_destroy(&remotes);
239     unixctl_server_destroy(unixctl);
240
241     if (run_process && process_exited(run_process)) {
242         int status = process_status(run_process);
243         if (status) {
244             ovs_fatal(0, "%s: child exited, %s",
245                       run_command, process_status_msg(status));
246         }
247     }
248
249     return 0;
250 }
251
252 static const struct db *
253 find_db(const struct db dbs[], size_t n_dbs, const char *db_name)
254 {
255     size_t i;
256
257     for (i = 0; i < n_dbs; i++) {
258         if (!strcmp(dbs[i].db->schema->name, db_name)) {
259             return &dbs[i];
260         }
261     }
262
263     return NULL;
264 }
265
266 static void
267 parse_db_column(const struct db dbs[], size_t n_dbs,
268                 const char *name_,
269                 const struct db **dbp,
270                 const struct ovsdb_table **tablep,
271                 const struct ovsdb_column **columnp)
272 {
273     const char *table_name, *column_name;
274     const struct ovsdb_column *column;
275     const struct ovsdb_table *table;
276     const char *tokens[3];
277     char *save_ptr = NULL;
278     const struct db *db;
279     char *name;
280
281     name = xstrdup(name_);
282     strtok_r(name, ":", &save_ptr); /* "db:" */
283     tokens[0] = strtok_r(NULL, ",", &save_ptr);
284     tokens[1] = strtok_r(NULL, ",", &save_ptr);
285     tokens[2] = strtok_r(NULL, ",", &save_ptr);
286     if (!tokens[0] || !tokens[1]) {
287         ovs_fatal(0, "\"%s\": invalid syntax", name_);
288     }
289     if (tokens[2]) {
290         const char *db_name = tokens[0];
291         table_name = tokens[1];
292         column_name = tokens[2];
293
294         db = find_db(dbs, n_dbs, tokens[0]);
295         if (!db) {
296             ovs_fatal(0, "\"%s\": no database named %s", name_, db_name);
297         }
298     } else {
299         if (n_dbs > 1) {
300             ovs_fatal(0, "\"%s\": database name must be specified (because "
301                       "multiple databases are configured)", name_);
302         }
303
304         table_name = tokens[0];
305         column_name = tokens[1];
306         db = &dbs[0];
307     }
308
309     table = ovsdb_get_table(db->db, table_name);
310     if (!table) {
311         ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
312     }
313
314     column = ovsdb_table_schema_get_column(table->schema, column_name);
315     if (!column) {
316         ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
317                   name_, table_name, column_name);
318     }
319     free(name);
320
321     *dbp = db;
322     *columnp = column;
323     *tablep = table;
324 }
325
326 static void
327 parse_db_string_column(const struct db dbs[], size_t n_dbs,
328                        const char *name,
329                        const struct db **dbp,
330                        const struct ovsdb_table **tablep,
331                        const struct ovsdb_column **columnp)
332 {
333     const struct ovsdb_column *column;
334     const struct ovsdb_table *table;
335     const struct db *db;
336
337     parse_db_column(dbs, n_dbs, name, &db, &table, &column);
338
339     if (column->type.key.type != OVSDB_TYPE_STRING
340         || column->type.value.type != OVSDB_TYPE_VOID) {
341         ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
342                   "not string or set of strings",
343                   name, table->schema->name, column->name);
344     }
345
346     *dbp = db;
347     *columnp = column;
348     *tablep = table;
349 }
350
351 static OVS_UNUSED const char *
352 query_db_string(const struct db dbs[], size_t n_dbs, const char *name)
353 {
354     if (!name || strncmp(name, "db:", 3)) {
355         return name;
356     } else {
357         const struct ovsdb_column *column;
358         const struct ovsdb_table *table;
359         const struct ovsdb_row *row;
360         const struct db *db;
361
362         parse_db_string_column(dbs, n_dbs, name, &db, &table, &column);
363
364         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
365             const struct ovsdb_datum *datum;
366             size_t i;
367
368             datum = &row->fields[column->index];
369             for (i = 0; i < datum->n; i++) {
370                 if (datum->keys[i].string[0]) {
371                     return datum->keys[i].string;
372                 }
373             }
374         }
375         return NULL;
376     }
377 }
378
379 static struct ovsdb_jsonrpc_options *
380 add_remote(struct shash *remotes, const char *target)
381 {
382     struct ovsdb_jsonrpc_options *options;
383
384     options = shash_find_data(remotes, target);
385     if (!options) {
386         options = ovsdb_jsonrpc_default_options(target);
387         shash_add(remotes, target, options);
388     }
389
390     return options;
391 }
392
393 static struct ovsdb_datum *
394 get_datum(struct ovsdb_row *row, const char *column_name,
395           const enum ovsdb_atomic_type key_type,
396           const enum ovsdb_atomic_type value_type,
397           const size_t n_max)
398 {
399     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
400     const struct ovsdb_table_schema *schema = row->table->schema;
401     const struct ovsdb_column *column;
402
403     column = ovsdb_table_schema_get_column(schema, column_name);
404     if (!column) {
405         VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
406                     schema->name, column_name);
407         return NULL;
408     }
409
410     if (column->type.key.type != key_type
411         || column->type.value.type != value_type
412         || column->type.n_max != n_max) {
413         if (!VLOG_DROP_DBG(&rl)) {
414             char *type_name = ovsdb_type_to_english(&column->type);
415             VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
416                      "key type %s, value type %s, max elements %zd.",
417                      schema->name, column_name, type_name,
418                      ovsdb_atomic_type_to_string(key_type),
419                      ovsdb_atomic_type_to_string(value_type),
420                      n_max);
421             free(type_name);
422         }
423         return NULL;
424     }
425
426     return &row->fields[column->index];
427 }
428
429 /* Read string-string key-values from a map.  Returns the value associated with
430  * 'key', if found, or NULL */
431 static const char *
432 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
433                        const char *key)
434 {
435     const struct ovsdb_datum *datum;
436     union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
437     size_t i;
438
439     datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
440                       OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
441
442     if (!datum) {
443         return NULL;
444     }
445
446     for (i = 0; i < datum->n; i++) {
447         atom_key = &datum->keys[i];
448         if (!strcmp(atom_key->string, key)){
449             atom_value = &datum->values[i];
450             break;
451         }
452     }
453
454     return atom_value ? atom_value->string : NULL;
455 }
456
457 static const union ovsdb_atom *
458 read_column(const struct ovsdb_row *row, const char *column_name,
459             enum ovsdb_atomic_type type)
460 {
461     const struct ovsdb_datum *datum;
462
463     datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
464                       OVSDB_TYPE_VOID, 1);
465     return datum && datum->n ? datum->keys : NULL;
466 }
467
468 static bool
469 read_integer_column(const struct ovsdb_row *row, const char *column_name,
470                     long long int *integerp)
471 {
472     const union ovsdb_atom *atom;
473
474     atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
475     *integerp = atom ? atom->integer : 0;
476     return atom != NULL;
477 }
478
479 static bool
480 read_string_column(const struct ovsdb_row *row, const char *column_name,
481                    const char **stringp)
482 {
483     const union ovsdb_atom *atom;
484
485     atom = read_column(row, column_name, OVSDB_TYPE_STRING);
486     *stringp = atom ? atom->string : NULL;
487     return atom != NULL;
488 }
489
490 static void
491 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
492 {
493     const struct ovsdb_column *column;
494     struct ovsdb_datum *datum;
495
496     column = ovsdb_table_schema_get_column(row->table->schema, column_name);
497     datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
498                       OVSDB_TYPE_VOID, 1);
499     if (!datum) {
500         return;
501     }
502
503     if (datum->n != 1) {
504         ovsdb_datum_destroy(datum, &column->type);
505
506         datum->n = 1;
507         datum->keys = xmalloc(sizeof *datum->keys);
508         datum->values = NULL;
509     }
510
511     datum->keys[0].boolean = value;
512 }
513
514 static void
515 write_string_string_column(struct ovsdb_row *row, const char *column_name,
516                            char **keys, char **values, size_t n)
517 {
518     const struct ovsdb_column *column;
519     struct ovsdb_datum *datum;
520     size_t i;
521
522     column = ovsdb_table_schema_get_column(row->table->schema, column_name);
523     datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
524                       UINT_MAX);
525     if (!datum) {
526         for (i = 0; i < n; i++) {
527             free(keys[i]);
528             free(values[i]);
529         }
530         return;
531     }
532
533     /* Free existing data. */
534     ovsdb_datum_destroy(datum, &column->type);
535
536     /* Allocate space for new values. */
537     datum->n = n;
538     datum->keys = xmalloc(n * sizeof *datum->keys);
539     datum->values = xmalloc(n * sizeof *datum->values);
540
541     for (i = 0; i < n; ++i) {
542         datum->keys[i].string = keys[i];
543         datum->values[i].string = values[i];
544     }
545
546     /* Sort and check constraints. */
547     ovsdb_datum_sort_assert(datum, column->type.key.type);
548 }
549
550 /* Adds a remote and options to 'remotes', based on the Manager table row in
551  * 'row'. */
552 static void
553 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
554 {
555     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
556     struct ovsdb_jsonrpc_options *options;
557     long long int max_backoff, probe_interval;
558     const char *target, *dscp_string;
559
560     if (!read_string_column(row, "target", &target) || !target) {
561         VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
562                      row->table->schema->name);
563         return;
564     }
565
566     options = add_remote(remotes, target);
567     if (read_integer_column(row, "max_backoff", &max_backoff)) {
568         options->max_backoff = max_backoff;
569     }
570     if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
571         options->probe_interval = probe_interval;
572     }
573
574     options->dscp = DSCP_DEFAULT;
575     dscp_string = read_map_string_column(row, "other_config", "dscp");
576     if (dscp_string) {
577         int dscp = atoi(dscp_string);
578         if (dscp >= 0 && dscp <= 63) {
579             options->dscp = dscp;
580         }
581     }
582 }
583
584 static void
585 query_db_remotes(const char *name, const struct db dbs[], size_t n_dbs,
586                  struct shash *remotes)
587 {
588     const struct ovsdb_column *column;
589     const struct ovsdb_table *table;
590     const struct ovsdb_row *row;
591     const struct db *db;
592
593     parse_db_column(dbs, n_dbs, name, &db, &table, &column);
594
595     if (column->type.key.type == OVSDB_TYPE_STRING
596         && column->type.value.type == OVSDB_TYPE_VOID) {
597         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
598             const struct ovsdb_datum *datum;
599             size_t i;
600
601             datum = &row->fields[column->index];
602             for (i = 0; i < datum->n; i++) {
603                 add_remote(remotes, datum->keys[i].string);
604             }
605         }
606     } else if (column->type.key.type == OVSDB_TYPE_UUID
607                && column->type.key.u.uuid.refTable
608                && column->type.value.type == OVSDB_TYPE_VOID) {
609         const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
610         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
611             const struct ovsdb_datum *datum;
612             size_t i;
613
614             datum = &row->fields[column->index];
615             for (i = 0; i < datum->n; i++) {
616                 const struct ovsdb_row *ref_row;
617
618                 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
619                 if (ref_row) {
620                     add_manager_options(remotes, ref_row);
621                 }
622             }
623         }
624     }
625 }
626
627 static void
628 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
629                   const struct ovsdb_jsonrpc_server *jsonrpc)
630 {
631     struct ovsdb_jsonrpc_remote_status status;
632     struct ovsdb_row *rw_row;
633     const char *target;
634     char *keys[8], *values[8];
635     size_t n = 0;
636
637     /* Get the "target" (protocol/host/port) spec. */
638     if (!read_string_column(row, "target", &target)) {
639         /* Bad remote spec or incorrect schema. */
640         return;
641     }
642     rw_row = ovsdb_txn_row_modify(txn, row);
643     ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
644
645     /* Update status information columns. */
646     write_bool_column(rw_row, "is_connected", status.is_connected);
647
648     if (status.state) {
649         keys[n] = xstrdup("state");
650         values[n++] = xstrdup(status.state);
651     }
652     if (status.sec_since_connect != UINT_MAX) {
653         keys[n] = xstrdup("sec_since_connect");
654         values[n++] = xasprintf("%u", status.sec_since_connect);
655     }
656     if (status.sec_since_disconnect != UINT_MAX) {
657         keys[n] = xstrdup("sec_since_disconnect");
658         values[n++] = xasprintf("%u", status.sec_since_disconnect);
659     }
660     if (status.last_error) {
661         keys[n] = xstrdup("last_error");
662         values[n++] =
663             xstrdup(ovs_retval_to_string(status.last_error));
664     }
665     if (status.locks_held && status.locks_held[0]) {
666         keys[n] = xstrdup("locks_held");
667         values[n++] = xstrdup(status.locks_held);
668     }
669     if (status.locks_waiting && status.locks_waiting[0]) {
670         keys[n] = xstrdup("locks_waiting");
671         values[n++] = xstrdup(status.locks_waiting);
672     }
673     if (status.locks_lost && status.locks_lost[0]) {
674         keys[n] = xstrdup("locks_lost");
675         values[n++] = xstrdup(status.locks_lost);
676     }
677     if (status.n_connections > 1) {
678         keys[n] = xstrdup("n_connections");
679         values[n++] = xasprintf("%d", status.n_connections);
680     }
681     write_string_string_column(rw_row, "status", keys, values, n);
682
683     ovsdb_jsonrpc_server_free_remote_status(&status);
684 }
685
686 static void
687 update_remote_rows(const struct db dbs[], size_t n_dbs,
688                    const char *remote_name,
689                    const struct ovsdb_jsonrpc_server *jsonrpc)
690 {
691     const struct ovsdb_table *table, *ref_table;
692     const struct ovsdb_column *column;
693     const struct ovsdb_row *row;
694     const struct db *db;
695
696     if (strncmp("db:", remote_name, 3)) {
697         return;
698     }
699
700     parse_db_column(dbs, n_dbs, remote_name, &db, &table, &column);
701
702     if (column->type.key.type != OVSDB_TYPE_UUID
703         || !column->type.key.u.uuid.refTable
704         || column->type.value.type != OVSDB_TYPE_VOID) {
705         return;
706     }
707
708     ref_table = column->type.key.u.uuid.refTable;
709
710     HMAP_FOR_EACH (row, hmap_node, &table->rows) {
711         const struct ovsdb_datum *datum;
712         size_t i;
713
714         datum = &row->fields[column->index];
715         for (i = 0; i < datum->n; i++) {
716             const struct ovsdb_row *ref_row;
717
718             ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
719             if (ref_row) {
720                 update_remote_row(ref_row, db->txn, jsonrpc);
721             }
722         }
723     }
724 }
725
726 static void
727 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
728                      const struct sset *remotes,
729                      struct db dbs[], size_t n_dbs)
730 {
731     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
732     const char *remote;
733     size_t i;
734
735     for (i = 0; i < n_dbs; i++) {
736         dbs[i].txn = ovsdb_txn_create(dbs[i].db);
737     }
738
739     /* Iterate over --remote arguments given on command line. */
740     SSET_FOR_EACH (remote, remotes) {
741         update_remote_rows(dbs, n_dbs, remote, jsonrpc);
742     }
743
744     for (i = 0; i < n_dbs; i++) {
745         struct ovsdb_error *error = ovsdb_txn_commit(dbs[i].txn, false);
746         if (error) {
747             VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
748                         ovsdb_error_to_string(error));
749             ovsdb_error_destroy(error);
750         }
751     }
752 }
753
754 /* Reconfigures ovsdb-server based on information in the database. */
755 static void
756 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
757                     const struct db dbs[], size_t n_dbs, struct sset *remotes)
758 {
759     struct shash resolved_remotes;
760     const char *name;
761
762     /* Configure remotes. */
763     shash_init(&resolved_remotes);
764     SSET_FOR_EACH (name, remotes) {
765         if (!strncmp(name, "db:", 3)) {
766             query_db_remotes(name, dbs, n_dbs, &resolved_remotes);
767         } else {
768             add_remote(&resolved_remotes, name);
769         }
770     }
771     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
772     shash_destroy_free_data(&resolved_remotes);
773
774     /* Configure SSL. */
775     stream_ssl_set_key_and_cert(query_db_string(dbs, n_dbs, private_key_file),
776                                 query_db_string(dbs, n_dbs, certificate_file));
777     stream_ssl_set_ca_cert_file(query_db_string(dbs, n_dbs, ca_cert_file),
778                                 bootstrap_ca_cert);
779 }
780
781 static void
782 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
783                   const char *argv[] OVS_UNUSED,
784                   void *exiting_)
785 {
786     bool *exiting = exiting_;
787     *exiting = true;
788     unixctl_command_reply(conn, NULL);
789 }
790
791 static void
792 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
793                      const char *argv[], void *dbs_)
794 {
795     struct db *dbs = dbs_;
796     struct ds reply;
797     struct db *db;
798     int n = 0;
799
800     ds_init(&reply);
801     for (db = dbs; db->filename != NULL; db++) {
802         const char *name = db->db->schema->name;
803
804         if (argc < 2 || !strcmp(argv[1], name)) {
805             struct ovsdb_error *error;
806
807             VLOG_INFO("compacting %s database by user request", name);
808
809             error = ovsdb_file_compact(db->file);
810             if (error) {
811                 char *s = ovsdb_error_to_string(error);
812                 ds_put_format(&reply, "%s\n", s);
813                 free(s);
814             }
815
816             n++;
817         }
818     }
819
820     if (!n) {
821         unixctl_command_reply_error(conn, "no database by that name");
822     } else if (reply.length) {
823         unixctl_command_reply_error(conn, ds_cstr(&reply));
824     } else {
825         unixctl_command_reply(conn, NULL);
826     }
827     ds_destroy(&reply);
828 }
829
830 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
831  * connections and reconnect. */
832 static void
833 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
834                        const char *argv[] OVS_UNUSED, void *jsonrpc_)
835 {
836     struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
837
838     ovsdb_jsonrpc_server_reconnect(jsonrpc);
839     unixctl_command_reply(conn, NULL);
840 }
841
842 static void
843 parse_options(int *argcp, char **argvp[],
844               struct sset *remotes, char **unixctl_pathp, char **run_command)
845 {
846     enum {
847         OPT_REMOTE = UCHAR_MAX + 1,
848         OPT_UNIXCTL,
849         OPT_RUN,
850         OPT_BOOTSTRAP_CA_CERT,
851         OPT_ENABLE_DUMMY,
852         VLOG_OPTION_ENUMS,
853         LEAK_CHECKER_OPTION_ENUMS,
854         DAEMON_OPTION_ENUMS
855     };
856     static struct option long_options[] = {
857         {"remote",      required_argument, NULL, OPT_REMOTE},
858         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
859         {"run",         required_argument, NULL, OPT_RUN},
860         {"help",        no_argument, NULL, 'h'},
861         {"version",     no_argument, NULL, 'V'},
862         DAEMON_LONG_OPTIONS,
863         VLOG_LONG_OPTIONS,
864         LEAK_CHECKER_LONG_OPTIONS,
865         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
866         {"private-key", required_argument, NULL, 'p'},
867         {"certificate", required_argument, NULL, 'c'},
868         {"ca-cert",     required_argument, NULL, 'C'},
869         {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
870         {NULL, 0, NULL, 0},
871     };
872     char *short_options = long_options_to_short_options(long_options);
873     int argc = *argcp;
874     char **argv = *argvp;
875
876     sset_init(remotes);
877     for (;;) {
878         int c;
879
880         c = getopt_long(argc, argv, short_options, long_options, NULL);
881         if (c == -1) {
882             break;
883         }
884
885         switch (c) {
886         case OPT_REMOTE:
887             sset_add(remotes, optarg);
888             break;
889
890         case OPT_UNIXCTL:
891             *unixctl_pathp = optarg;
892             break;
893
894         case OPT_RUN:
895             *run_command = optarg;
896             break;
897
898         case 'h':
899             usage();
900
901         case 'V':
902             ovs_print_version(0, 0);
903             exit(EXIT_SUCCESS);
904
905         VLOG_OPTION_HANDLERS
906         DAEMON_OPTION_HANDLERS
907         LEAK_CHECKER_OPTION_HANDLERS
908
909         case 'p':
910             private_key_file = optarg;
911             break;
912
913         case 'c':
914             certificate_file = optarg;
915             break;
916
917         case 'C':
918             ca_cert_file = optarg;
919             bootstrap_ca_cert = false;
920             break;
921
922         case OPT_BOOTSTRAP_CA_CERT:
923             ca_cert_file = optarg;
924             bootstrap_ca_cert = true;
925             break;
926
927         case OPT_ENABLE_DUMMY:
928             dummy_enable(optarg && !strcmp(optarg, "override"));
929             break;
930
931         case '?':
932             exit(EXIT_FAILURE);
933
934         default:
935             abort();
936         }
937     }
938     free(short_options);
939
940     *argcp -= optind;
941     *argvp += optind;
942 }
943
944 static void
945 usage(void)
946 {
947     printf("%s: Open vSwitch database server\n"
948            "usage: %s [OPTIONS] [DATABASE...]\n"
949            "where each DATABASE is a database file in ovsdb format.\n"
950            "The default DATABASE, if none is given, is\n%s/conf.db.\n",
951            program_name, program_name, ovs_dbdir());
952     printf("\nJSON-RPC options (may be specified any number of times):\n"
953            "  --remote=REMOTE         connect or listen to REMOTE\n");
954     stream_usage("JSON-RPC", true, true, true);
955     daemon_usage();
956     vlog_usage();
957     printf("\nOther options:\n"
958            "  --run COMMAND           run COMMAND as subprocess then exit\n"
959            "  --unixctl=SOCKET        override default control socket name\n"
960            "  -h, --help              display this help message\n"
961            "  -V, --version           display version information\n");
962     leak_checker_usage();
963     exit(EXIT_SUCCESS);
964 }