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