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