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