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