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