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