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