]> git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/ovsdb-server.c
json: Move from lib to include/openvswitch.
[mirror_ovs.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 <errno.h>
19 #include <getopt.h>
20 #include <inttypes.h>
21 #include <signal.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "daemon.h"
28 #include "dirs.h"
29 #include "openvswitch/dynamic-string.h"
30 #include "fatal-signal.h"
31 #include "file.h"
32 #include "hash.h"
33 #include "openvswitch/json.h"
34 #include "jsonrpc.h"
35 #include "jsonrpc-server.h"
36 #include "openvswitch/list.h"
37 #include "memory.h"
38 #include "monitor.h"
39 #include "ovsdb.h"
40 #include "ovsdb-data.h"
41 #include "ovsdb-types.h"
42 #include "ovsdb-error.h"
43 #include "poll-loop.h"
44 #include "process.h"
45 #include "replication.h"
46 #include "row.h"
47 #include "simap.h"
48 #include "openvswitch/shash.h"
49 #include "stream-ssl.h"
50 #include "stream.h"
51 #include "sset.h"
52 #include "table.h"
53 #include "timeval.h"
54 #include "transaction.h"
55 #include "trigger.h"
56 #include "util.h"
57 #include "unixctl.h"
58 #include "perf-counter.h"
59 #include "openvswitch/vlog.h"
60
61 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
62
63 struct db;
64
65 /* SSL configuration. */
66 static char *private_key_file;
67 static char *certificate_file;
68 static char *ca_cert_file;
69 static bool bootstrap_ca_cert;
70
71 /* Replication configuration. */
72 static bool connect_to_remote_server;
73
74 static unixctl_cb_func ovsdb_server_exit;
75 static unixctl_cb_func ovsdb_server_compact;
76 static unixctl_cb_func ovsdb_server_reconnect;
77 static unixctl_cb_func ovsdb_server_perf_counters_clear;
78 static unixctl_cb_func ovsdb_server_perf_counters_show;
79 static unixctl_cb_func ovsdb_server_disable_monitor_cond;
80 static unixctl_cb_func ovsdb_server_set_remote_ovsdb_server;
81 static unixctl_cb_func ovsdb_server_get_remote_ovsdb_server;
82 static unixctl_cb_func ovsdb_server_connect_remote_ovsdb_server;
83 static unixctl_cb_func ovsdb_server_disconnect_remote_ovsdb_server;
84 static unixctl_cb_func ovsdb_server_set_sync_excluded_tables;
85 static unixctl_cb_func ovsdb_server_get_sync_excluded_tables;
86
87 struct server_config {
88 struct sset *remotes;
89 struct shash *all_dbs;
90 FILE *config_tmpfile;
91 struct ovsdb_jsonrpc_server *jsonrpc;
92 };
93 static unixctl_cb_func ovsdb_server_add_remote;
94 static unixctl_cb_func ovsdb_server_remove_remote;
95 static unixctl_cb_func ovsdb_server_list_remotes;
96
97 static unixctl_cb_func ovsdb_server_add_database;
98 static unixctl_cb_func ovsdb_server_remove_database;
99 static unixctl_cb_func ovsdb_server_list_databases;
100
101 static char *open_db(struct server_config *config, const char *filename);
102 static void close_db(struct db *db);
103
104 static void parse_options(int *argc, char **argvp[],
105 struct sset *remotes, char **unixctl_pathp,
106 char **run_command);
107 OVS_NO_RETURN static void usage(void);
108
109 static char *reconfigure_remotes(struct ovsdb_jsonrpc_server *,
110 const struct shash *all_dbs,
111 struct sset *remotes);
112 static char *reconfigure_ssl(const struct shash *all_dbs);
113 static void report_error_if_changed(char *error, char **last_errorp);
114
115 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
116 const struct sset *remotes,
117 struct shash *all_dbs);
118
119 static void save_config__(FILE *config_file, const struct sset *remotes,
120 const struct sset *db_filenames);
121 static void save_config(struct server_config *);
122 static void load_config(FILE *config_file, struct sset *remotes,
123 struct sset *db_filenames);
124
125 static void
126 main_loop(struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs,
127 struct unixctl_server *unixctl, struct sset *remotes,
128 struct process *run_process, bool *exiting)
129 {
130 char *remotes_error, *ssl_error;
131 struct shash_node *node;
132 long long int status_timer = LLONG_MIN;
133
134 *exiting = false;
135 ssl_error = NULL;
136 remotes_error = NULL;
137 while (!*exiting) {
138 memory_run();
139 if (memory_should_report()) {
140 struct simap usage;
141
142 simap_init(&usage);
143 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
144 ovsdb_monitor_get_memory_usage(&usage);
145 SHASH_FOR_EACH(node, all_dbs) {
146 struct db *db = node->data;
147 ovsdb_get_memory_usage(db->db, &usage);
148 }
149 memory_report(&usage);
150 simap_destroy(&usage);
151 }
152
153 /* Run unixctl_server_run() before reconfigure_remotes() because
154 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
155 * the set of remotes that reconfigure_remotes() uses. */
156 unixctl_server_run(unixctl);
157
158 report_error_if_changed(
159 reconfigure_remotes(jsonrpc, all_dbs, remotes),
160 &remotes_error);
161 report_error_if_changed(reconfigure_ssl(all_dbs), &ssl_error);
162 ovsdb_jsonrpc_server_run(jsonrpc);
163
164 if (connect_to_remote_server) {
165 replication_run(all_dbs);
166 }
167
168 SHASH_FOR_EACH(node, all_dbs) {
169 struct db *db = node->data;
170 ovsdb_trigger_run(db->db, time_msec());
171 }
172 if (run_process) {
173 process_run();
174 if (process_exited(run_process)) {
175 *exiting = true;
176 }
177 }
178
179 /* update Manager status(es) every 2.5 seconds */
180 if (time_msec() >= status_timer) {
181 status_timer = time_msec() + 2500;
182 update_remote_status(jsonrpc, remotes, all_dbs);
183 }
184
185 memory_wait();
186 ovsdb_jsonrpc_server_wait(jsonrpc);
187 unixctl_server_wait(unixctl);
188 SHASH_FOR_EACH(node, all_dbs) {
189 struct db *db = node->data;
190 ovsdb_trigger_wait(db->db, time_msec());
191 }
192 if (run_process) {
193 process_wait(run_process);
194 }
195 if (*exiting) {
196 poll_immediate_wake();
197 }
198 poll_timer_wait_until(status_timer);
199 poll_block();
200 if (should_service_stop()) {
201 *exiting = true;
202 }
203 }
204
205 free(remotes_error);
206 }
207
208 int
209 main(int argc, char *argv[])
210 {
211 char *unixctl_path = NULL;
212 char *run_command = NULL;
213 struct unixctl_server *unixctl;
214 struct ovsdb_jsonrpc_server *jsonrpc;
215 struct sset remotes, db_filenames;
216 const char *db_filename;
217 struct process *run_process;
218 bool exiting;
219 int retval;
220 FILE *config_tmpfile;
221 struct server_config server_config;
222 struct shash all_dbs;
223 struct shash_node *node, *next;
224 char *error;
225 int i;
226
227 ovs_cmdl_proctitle_init(argc, argv);
228 set_program_name(argv[0]);
229 service_start(&argc, &argv);
230 fatal_ignore_sigpipe();
231 process_init();
232
233 parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
234 daemon_become_new_user(false);
235
236 /* Create and initialize 'config_tmpfile' as a temporary file to hold
237 * ovsdb-server's most basic configuration, and then save our initial
238 * configuration to it. When --monitor is used, this preserves the effects
239 * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
240 * new configuration) across crashes. */
241 config_tmpfile = tmpfile();
242 if (!config_tmpfile) {
243 ovs_fatal(errno, "failed to create temporary file");
244 }
245
246 sset_init(&db_filenames);
247 if (argc > 0) {
248 for (i = 0; i < argc; i++) {
249 sset_add(&db_filenames, argv[i]);
250 }
251 } else {
252 char *default_db = xasprintf("%s/conf.db", ovs_dbdir());
253 sset_add(&db_filenames, default_db);
254 free(default_db);
255 }
256
257 server_config.remotes = &remotes;
258 server_config.config_tmpfile = config_tmpfile;
259
260 save_config__(config_tmpfile, &remotes, &db_filenames);
261
262 daemonize_start(false);
263
264 /* Load the saved config. */
265 load_config(config_tmpfile, &remotes, &db_filenames);
266 jsonrpc = ovsdb_jsonrpc_server_create();
267
268 shash_init(&all_dbs);
269 server_config.all_dbs = &all_dbs;
270 server_config.jsonrpc = jsonrpc;
271
272 perf_counters_init();
273
274 SSET_FOR_EACH (db_filename, &db_filenames) {
275 error = open_db(&server_config, db_filename);
276 if (error) {
277 ovs_fatal(0, "%s", error);
278 }
279 }
280
281 error = reconfigure_remotes(jsonrpc, &all_dbs, &remotes);
282 if (!error) {
283 error = reconfigure_ssl(&all_dbs);
284 }
285 if (error) {
286 ovs_fatal(0, "%s", error);
287 }
288
289 retval = unixctl_server_create(unixctl_path, &unixctl);
290 if (retval) {
291 exit(EXIT_FAILURE);
292 }
293
294 if (run_command) {
295 char *run_argv[4];
296
297 run_argv[0] = "/bin/sh";
298 run_argv[1] = "-c";
299 run_argv[2] = run_command;
300 run_argv[3] = NULL;
301
302 retval = process_start(run_argv, &run_process);
303 if (retval) {
304 ovs_fatal(retval, "%s: process failed to start", run_command);
305 }
306 } else {
307 run_process = NULL;
308 }
309
310 daemonize_complete();
311
312 if (!run_command) {
313 /* ovsdb-server is usually a long-running process, in which case it
314 * makes plenty of sense to log the version, but --run makes
315 * ovsdb-server more like a command-line tool, so skip it. */
316 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
317 }
318
319 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
320 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
321 ovsdb_server_compact, &all_dbs);
322 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
323 ovsdb_server_reconnect, jsonrpc);
324
325 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
326 ovsdb_server_add_remote, &server_config);
327 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
328 ovsdb_server_remove_remote, &server_config);
329 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
330 ovsdb_server_list_remotes, &remotes);
331
332 unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
333 ovsdb_server_add_database, &server_config);
334 unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
335 ovsdb_server_remove_database, &server_config);
336 unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
337 ovsdb_server_list_databases, &all_dbs);
338 unixctl_command_register("ovsdb-server/perf-counters-show", "", 0, 0,
339 ovsdb_server_perf_counters_show, NULL);
340 unixctl_command_register("ovsdb-server/perf-counters-clear", "", 0, 0,
341 ovsdb_server_perf_counters_clear, NULL);
342
343 unixctl_command_register("ovsdb-server/set-remote-ovsdb-server", "", 0, 1,
344 ovsdb_server_set_remote_ovsdb_server, NULL);
345 unixctl_command_register("ovsdb-server/get-remote-ovsdb-server", "", 0, 0,
346 ovsdb_server_get_remote_ovsdb_server, NULL);
347 unixctl_command_register("ovsdb-server/connect-remote-ovsdb-server", "", 0, 0,
348 ovsdb_server_connect_remote_ovsdb_server, NULL);
349 unixctl_command_register("ovsdb-server/disconnect-remote-ovsdb-server", "", 0, 0,
350 ovsdb_server_disconnect_remote_ovsdb_server, NULL);
351 unixctl_command_register("ovsdb-server/set-sync-excluded-tables", "", 0, 1,
352 ovsdb_server_set_sync_excluded_tables, NULL);
353 unixctl_command_register("ovsdb-server/get-sync-excluded-tables", "", 0, 0,
354 ovsdb_server_get_sync_excluded_tables, NULL);
355
356 /* Simulate the behavior of OVS release prior to version 2.5 that
357 * does not support the monitor_cond method. */
358 unixctl_command_register("ovsdb-server/disable-monitor-cond", "", 0, 0,
359 ovsdb_server_disable_monitor_cond, jsonrpc);
360
361 main_loop(jsonrpc, &all_dbs, unixctl, &remotes, run_process, &exiting);
362
363 ovsdb_jsonrpc_server_destroy(jsonrpc);
364 SHASH_FOR_EACH_SAFE(node, next, &all_dbs) {
365 struct db *db = node->data;
366 close_db(db);
367 shash_delete(&all_dbs, node);
368 }
369 shash_destroy(&all_dbs);
370 sset_destroy(&remotes);
371 sset_destroy(&db_filenames);
372 unixctl_server_destroy(unixctl);
373 disconnect_remote_server();
374
375 if (run_process && process_exited(run_process)) {
376 int status = process_status(run_process);
377 if (status) {
378 ovs_fatal(0, "%s: child exited, %s",
379 run_command, process_status_msg(status));
380 }
381 }
382 perf_counters_destroy();
383 service_stop();
384 return 0;
385 }
386
387 /* Returns true if 'filename' is known to be already open as a database,
388 * false if not.
389 *
390 * "False negatives" are possible. */
391 static bool
392 is_already_open(struct server_config *config OVS_UNUSED,
393 const char *filename OVS_UNUSED)
394 {
395 #ifndef _WIN32
396 struct stat s;
397
398 if (!stat(filename, &s)) {
399 struct shash_node *node;
400
401 SHASH_FOR_EACH (node, config->all_dbs) {
402 struct db *db = node->data;
403 struct stat s2;
404
405 if (!stat(db->filename, &s2)
406 && s.st_dev == s2.st_dev
407 && s.st_ino == s2.st_ino) {
408 return true;
409 }
410 }
411 }
412 #endif /* !_WIN32 */
413
414 return false;
415 }
416
417 static void
418 close_db(struct db *db)
419 {
420 ovsdb_destroy(db->db);
421 free(db->filename);
422 free(db);
423 }
424
425 static char *
426 open_db(struct server_config *config, const char *filename)
427 {
428 struct ovsdb_error *db_error;
429 struct db *db;
430 char *error;
431
432 /* If we know that the file is already open, return a good error message.
433 * Otherwise, if the file is open, we'll fail later on with a harder to
434 * interpret file locking error. */
435 if (is_already_open(config, filename)) {
436 return xasprintf("%s: already open", filename);
437 }
438
439 db = xzalloc(sizeof *db);
440 db->filename = xstrdup(filename);
441
442 db_error = ovsdb_file_open(db->filename, false, &db->db, &db->file);
443 if (db_error) {
444 error = ovsdb_error_to_string(db_error);
445 } else if (!ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db)) {
446 error = xasprintf("%s: duplicate database name", db->db->schema->name);
447 } else {
448 shash_add_assert(config->all_dbs, db->db->schema->name, db);
449 return NULL;
450 }
451
452 ovsdb_error_destroy(db_error);
453 close_db(db);
454 return error;
455 }
456
457 static char * OVS_WARN_UNUSED_RESULT
458 parse_db_column__(const struct shash *all_dbs,
459 const char *name_, char *name,
460 const struct db **dbp,
461 const struct ovsdb_table **tablep,
462 const struct ovsdb_column **columnp)
463 {
464 const char *db_name, *table_name, *column_name;
465 const struct ovsdb_column *column;
466 const struct ovsdb_table *table;
467 const char *tokens[3];
468 char *save_ptr = NULL;
469 const struct db *db;
470
471 *dbp = NULL;
472 *tablep = NULL;
473 *columnp = NULL;
474
475 strtok_r(name, ":", &save_ptr); /* "db:" */
476 tokens[0] = strtok_r(NULL, ",", &save_ptr);
477 tokens[1] = strtok_r(NULL, ",", &save_ptr);
478 tokens[2] = strtok_r(NULL, ",", &save_ptr);
479 if (!tokens[0] || !tokens[1] || !tokens[2]) {
480 return xasprintf("\"%s\": invalid syntax", name_);
481 }
482
483 db_name = tokens[0];
484 table_name = tokens[1];
485 column_name = tokens[2];
486
487 db = find_db(all_dbs, tokens[0]);
488 if (!db) {
489 return xasprintf("\"%s\": no database named %s", name_, db_name);
490 }
491
492 table = ovsdb_get_table(db->db, table_name);
493 if (!table) {
494 return xasprintf("\"%s\": no table named %s", name_, table_name);
495 }
496
497 column = ovsdb_table_schema_get_column(table->schema, column_name);
498 if (!column) {
499 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
500 name_, table_name, column_name);
501 }
502
503 *dbp = db;
504 *columnp = column;
505 *tablep = table;
506 return NULL;
507 }
508
509 /* Returns NULL if successful, otherwise a malloc()'d string describing the
510 * error. */
511 static char * OVS_WARN_UNUSED_RESULT
512 parse_db_column(const struct shash *all_dbs,
513 const char *name_,
514 const struct db **dbp,
515 const struct ovsdb_table **tablep,
516 const struct ovsdb_column **columnp)
517 {
518 char *name = xstrdup(name_);
519 char *retval = parse_db_column__(all_dbs, name_, name,
520 dbp, tablep, columnp);
521 free(name);
522 return retval;
523 }
524
525 /* Returns NULL if successful, otherwise a malloc()'d string describing the
526 * error. */
527 static char * OVS_WARN_UNUSED_RESULT
528 parse_db_string_column(const struct shash *all_dbs,
529 const char *name,
530 const struct db **dbp,
531 const struct ovsdb_table **tablep,
532 const struct ovsdb_column **columnp)
533 {
534 char *retval;
535
536 retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
537 if (retval) {
538 return retval;
539 }
540
541 if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
542 || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
543 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
544 "not string or set of strings",
545 name, (*tablep)->schema->name, (*columnp)->name);
546 }
547
548 return NULL;
549 }
550
551 static const char *
552 query_db_string(const struct shash *all_dbs, const char *name,
553 struct ds *errors)
554 {
555 if (!name || strncmp(name, "db:", 3)) {
556 return name;
557 } else {
558 const struct ovsdb_column *column;
559 const struct ovsdb_table *table;
560 const struct ovsdb_row *row;
561 const struct db *db;
562 char *retval;
563
564 retval = parse_db_string_column(all_dbs, name,
565 &db, &table, &column);
566 if (retval) {
567 ds_put_format(errors, "%s\n", retval);
568 free(retval);
569 return NULL;
570 }
571
572 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
573 const struct ovsdb_datum *datum;
574 size_t i;
575
576 datum = &row->fields[column->index];
577 for (i = 0; i < datum->n; i++) {
578 if (datum->keys[i].string[0]) {
579 return datum->keys[i].string;
580 }
581 }
582 }
583 return NULL;
584 }
585 }
586
587 static struct ovsdb_jsonrpc_options *
588 add_remote(struct shash *remotes, const char *target)
589 {
590 struct ovsdb_jsonrpc_options *options;
591
592 options = shash_find_data(remotes, target);
593 if (!options) {
594 options = ovsdb_jsonrpc_default_options(target);
595 shash_add(remotes, target, options);
596 }
597
598 return options;
599 }
600
601 static struct ovsdb_datum *
602 get_datum(struct ovsdb_row *row, const char *column_name,
603 const enum ovsdb_atomic_type key_type,
604 const enum ovsdb_atomic_type value_type,
605 const size_t n_max)
606 {
607 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
608 const struct ovsdb_table_schema *schema = row->table->schema;
609 const struct ovsdb_column *column;
610
611 column = ovsdb_table_schema_get_column(schema, column_name);
612 if (!column) {
613 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
614 schema->name, column_name);
615 return NULL;
616 }
617
618 if (column->type.key.type != key_type
619 || column->type.value.type != value_type
620 || column->type.n_max != n_max) {
621 if (!VLOG_DROP_DBG(&rl)) {
622 char *type_name = ovsdb_type_to_english(&column->type);
623 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
624 "key type %s, value type %s, max elements %"PRIuSIZE".",
625 schema->name, column_name, type_name,
626 ovsdb_atomic_type_to_string(key_type),
627 ovsdb_atomic_type_to_string(value_type),
628 n_max);
629 free(type_name);
630 }
631 return NULL;
632 }
633
634 return &row->fields[column->index];
635 }
636
637 /* Read string-string key-values from a map. Returns the value associated with
638 * 'key', if found, or NULL */
639 static const char *
640 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
641 const char *key)
642 {
643 const struct ovsdb_datum *datum;
644 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
645 size_t i;
646
647 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
648 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
649
650 if (!datum) {
651 return NULL;
652 }
653
654 for (i = 0; i < datum->n; i++) {
655 atom_key = &datum->keys[i];
656 if (!strcmp(atom_key->string, key)){
657 atom_value = &datum->values[i];
658 break;
659 }
660 }
661
662 return atom_value ? atom_value->string : NULL;
663 }
664
665 static const union ovsdb_atom *
666 read_column(const struct ovsdb_row *row, const char *column_name,
667 enum ovsdb_atomic_type type)
668 {
669 const struct ovsdb_datum *datum;
670
671 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
672 OVSDB_TYPE_VOID, 1);
673 return datum && datum->n ? datum->keys : NULL;
674 }
675
676 static bool
677 read_integer_column(const struct ovsdb_row *row, const char *column_name,
678 long long int *integerp)
679 {
680 const union ovsdb_atom *atom;
681
682 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
683 *integerp = atom ? atom->integer : 0;
684 return atom != NULL;
685 }
686
687 static bool
688 read_string_column(const struct ovsdb_row *row, const char *column_name,
689 const char **stringp)
690 {
691 const union ovsdb_atom *atom;
692
693 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
694 *stringp = atom ? atom->string : NULL;
695 return atom != NULL;
696 }
697
698 static void
699 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
700 {
701 const struct ovsdb_column *column;
702 struct ovsdb_datum *datum;
703
704 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
705 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
706 OVSDB_TYPE_VOID, 1);
707 if (!datum) {
708 return;
709 }
710
711 if (datum->n != 1) {
712 ovsdb_datum_destroy(datum, &column->type);
713
714 datum->n = 1;
715 datum->keys = xmalloc(sizeof *datum->keys);
716 datum->values = NULL;
717 }
718
719 datum->keys[0].boolean = value;
720 }
721
722 static void
723 write_string_string_column(struct ovsdb_row *row, const char *column_name,
724 char **keys, char **values, size_t n)
725 {
726 const struct ovsdb_column *column;
727 struct ovsdb_datum *datum;
728 size_t i;
729
730 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
731 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
732 UINT_MAX);
733 if (!datum) {
734 for (i = 0; i < n; i++) {
735 free(keys[i]);
736 free(values[i]);
737 }
738 return;
739 }
740
741 /* Free existing data. */
742 ovsdb_datum_destroy(datum, &column->type);
743
744 /* Allocate space for new values. */
745 datum->n = n;
746 datum->keys = xmalloc(n * sizeof *datum->keys);
747 datum->values = xmalloc(n * sizeof *datum->values);
748
749 for (i = 0; i < n; ++i) {
750 datum->keys[i].string = keys[i];
751 datum->values[i].string = values[i];
752 }
753
754 /* Sort and check constraints. */
755 ovsdb_datum_sort_assert(datum, column->type.key.type);
756 }
757
758 /* Adds a remote and options to 'remotes', based on the Manager table row in
759 * 'row'. */
760 static void
761 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
762 {
763 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
764 struct ovsdb_jsonrpc_options *options;
765 long long int max_backoff, probe_interval;
766 const char *target, *dscp_string;
767
768 if (!read_string_column(row, "target", &target) || !target) {
769 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
770 row->table->schema->name);
771 return;
772 }
773
774 options = add_remote(remotes, target);
775 if (read_integer_column(row, "max_backoff", &max_backoff)) {
776 options->max_backoff = max_backoff;
777 }
778 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
779 options->probe_interval = probe_interval;
780 }
781
782 options->dscp = DSCP_DEFAULT;
783 dscp_string = read_map_string_column(row, "other_config", "dscp");
784 if (dscp_string) {
785 int dscp = atoi(dscp_string);
786 if (dscp >= 0 && dscp <= 63) {
787 options->dscp = dscp;
788 }
789 }
790 }
791
792 static void
793 query_db_remotes(const char *name, const struct shash *all_dbs,
794 struct shash *remotes, struct ds *errors)
795 {
796 const struct ovsdb_column *column;
797 const struct ovsdb_table *table;
798 const struct ovsdb_row *row;
799 const struct db *db;
800 char *retval;
801
802 retval = parse_db_column(all_dbs, name, &db, &table, &column);
803 if (retval) {
804 ds_put_format(errors, "%s\n", retval);
805 free(retval);
806 return;
807 }
808
809 if (column->type.key.type == OVSDB_TYPE_STRING
810 && column->type.value.type == OVSDB_TYPE_VOID) {
811 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
812 const struct ovsdb_datum *datum;
813 size_t i;
814
815 datum = &row->fields[column->index];
816 for (i = 0; i < datum->n; i++) {
817 add_remote(remotes, datum->keys[i].string);
818 }
819 }
820 } else if (column->type.key.type == OVSDB_TYPE_UUID
821 && column->type.key.u.uuid.refTable
822 && column->type.value.type == OVSDB_TYPE_VOID) {
823 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
824 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
825 const struct ovsdb_datum *datum;
826 size_t i;
827
828 datum = &row->fields[column->index];
829 for (i = 0; i < datum->n; i++) {
830 const struct ovsdb_row *ref_row;
831
832 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
833 if (ref_row) {
834 add_manager_options(remotes, ref_row);
835 }
836 }
837 }
838 }
839 }
840
841 static void
842 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
843 const struct ovsdb_jsonrpc_server *jsonrpc)
844 {
845 struct ovsdb_jsonrpc_remote_status status;
846 struct ovsdb_row *rw_row;
847 const char *target;
848 char *keys[9], *values[9];
849 size_t n = 0;
850
851 /* Get the "target" (protocol/host/port) spec. */
852 if (!read_string_column(row, "target", &target)) {
853 /* Bad remote spec or incorrect schema. */
854 return;
855 }
856 rw_row = ovsdb_txn_row_modify(txn, row);
857 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
858
859 /* Update status information columns. */
860 write_bool_column(rw_row, "is_connected", status.is_connected);
861
862 if (status.state) {
863 keys[n] = xstrdup("state");
864 values[n++] = xstrdup(status.state);
865 }
866 if (status.sec_since_connect != UINT_MAX) {
867 keys[n] = xstrdup("sec_since_connect");
868 values[n++] = xasprintf("%u", status.sec_since_connect);
869 }
870 if (status.sec_since_disconnect != UINT_MAX) {
871 keys[n] = xstrdup("sec_since_disconnect");
872 values[n++] = xasprintf("%u", status.sec_since_disconnect);
873 }
874 if (status.last_error) {
875 keys[n] = xstrdup("last_error");
876 values[n++] =
877 xstrdup(ovs_retval_to_string(status.last_error));
878 }
879 if (status.locks_held && status.locks_held[0]) {
880 keys[n] = xstrdup("locks_held");
881 values[n++] = xstrdup(status.locks_held);
882 }
883 if (status.locks_waiting && status.locks_waiting[0]) {
884 keys[n] = xstrdup("locks_waiting");
885 values[n++] = xstrdup(status.locks_waiting);
886 }
887 if (status.locks_lost && status.locks_lost[0]) {
888 keys[n] = xstrdup("locks_lost");
889 values[n++] = xstrdup(status.locks_lost);
890 }
891 if (status.n_connections > 1) {
892 keys[n] = xstrdup("n_connections");
893 values[n++] = xasprintf("%d", status.n_connections);
894 }
895 if (status.bound_port != htons(0)) {
896 keys[n] = xstrdup("bound_port");
897 values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
898 }
899 write_string_string_column(rw_row, "status", keys, values, n);
900
901 ovsdb_jsonrpc_server_free_remote_status(&status);
902 }
903
904 static void
905 update_remote_rows(const struct shash *all_dbs,
906 const char *remote_name,
907 const struct ovsdb_jsonrpc_server *jsonrpc)
908 {
909 const struct ovsdb_table *table, *ref_table;
910 const struct ovsdb_column *column;
911 const struct ovsdb_row *row;
912 const struct db *db;
913 char *retval;
914
915 if (strncmp("db:", remote_name, 3)) {
916 return;
917 }
918
919 retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
920 if (retval) {
921 free(retval);
922 return;
923 }
924
925 if (column->type.key.type != OVSDB_TYPE_UUID
926 || !column->type.key.u.uuid.refTable
927 || column->type.value.type != OVSDB_TYPE_VOID) {
928 return;
929 }
930
931 ref_table = column->type.key.u.uuid.refTable;
932
933 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
934 const struct ovsdb_datum *datum;
935 size_t i;
936
937 datum = &row->fields[column->index];
938 for (i = 0; i < datum->n; i++) {
939 const struct ovsdb_row *ref_row;
940
941 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
942 if (ref_row) {
943 update_remote_row(ref_row, db->txn, jsonrpc);
944 }
945 }
946 }
947 }
948
949 static void
950 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
951 const struct sset *remotes,
952 struct shash *all_dbs)
953 {
954 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
955 const char *remote;
956 struct db *db;
957 struct shash_node *node;
958
959 SHASH_FOR_EACH(node, all_dbs) {
960 db = node->data;
961 db->txn = ovsdb_txn_create(db->db);
962 }
963
964 /* Iterate over --remote arguments given on command line. */
965 SSET_FOR_EACH (remote, remotes) {
966 update_remote_rows(all_dbs, remote, jsonrpc);
967 }
968
969 SHASH_FOR_EACH(node, all_dbs) {
970 struct ovsdb_error *error;
971 db = node->data;
972 error = ovsdb_txn_commit(db->txn, false);
973 if (error) {
974 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
975 ovsdb_error_to_string(error));
976 ovsdb_error_destroy(error);
977 }
978 }
979 }
980
981 /* Reconfigures ovsdb-server's remotes based on information in the database. */
982 static char *
983 reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
984 const struct shash *all_dbs, struct sset *remotes)
985 {
986 struct ds errors = DS_EMPTY_INITIALIZER;
987 struct shash resolved_remotes;
988 const char *name;
989
990 /* Configure remotes. */
991 shash_init(&resolved_remotes);
992 SSET_FOR_EACH (name, remotes) {
993 if (!strncmp(name, "db:", 3)) {
994 query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
995 } else {
996 add_remote(&resolved_remotes, name);
997 }
998 }
999 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
1000 shash_destroy_free_data(&resolved_remotes);
1001
1002 return errors.string;
1003 }
1004
1005 static char *
1006 reconfigure_ssl(const struct shash *all_dbs)
1007 {
1008 struct ds errors = DS_EMPTY_INITIALIZER;
1009 const char *resolved_private_key;
1010 const char *resolved_certificate;
1011 const char *resolved_ca_cert;
1012
1013 resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
1014 resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
1015 resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
1016
1017 stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
1018 stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
1019
1020 return errors.string;
1021 }
1022
1023 static void
1024 report_error_if_changed(char *error, char **last_errorp)
1025 {
1026 if (error) {
1027 if (!*last_errorp || strcmp(error, *last_errorp)) {
1028 VLOG_WARN("%s", error);
1029 free(*last_errorp);
1030 *last_errorp = error;
1031 return;
1032 }
1033 free(error);
1034 } else {
1035 free(*last_errorp);
1036 *last_errorp = NULL;
1037 }
1038 }
1039
1040 static void
1041 ovsdb_server_set_remote_ovsdb_server(struct unixctl_conn *conn,
1042 int argc OVS_UNUSED, const char *argv[],
1043 void *arg_ OVS_UNUSED)
1044 {
1045 set_remote_ovsdb_server(argv[1]);
1046 connect_to_remote_server = false;
1047 unixctl_command_reply(conn, NULL);
1048 }
1049
1050 static void
1051 ovsdb_server_get_remote_ovsdb_server(struct unixctl_conn *conn,
1052 int argc OVS_UNUSED,
1053 const char *argv[] OVS_UNUSED,
1054 void *arg_ OVS_UNUSED)
1055 {
1056 struct ds s;
1057 ds_init(&s);
1058
1059 ds_put_format(&s, "%s\n", get_remote_ovsdb_server());
1060
1061 unixctl_command_reply(conn, ds_cstr(&s));
1062 ds_destroy(&s);
1063 }
1064
1065 static void
1066 ovsdb_server_connect_remote_ovsdb_server(struct unixctl_conn *conn,
1067 int argc OVS_UNUSED,
1068 const char *argv[] OVS_UNUSED,
1069 void *arg_ OVS_UNUSED)
1070 {
1071 if (!connect_to_remote_server) {
1072 replication_init();
1073 connect_to_remote_server = true;
1074 }
1075 unixctl_command_reply(conn, NULL);
1076 }
1077
1078 static void
1079 ovsdb_server_disconnect_remote_ovsdb_server(struct unixctl_conn *conn,
1080 int argc OVS_UNUSED,
1081 const char *argv[] OVS_UNUSED,
1082 void *arg_ OVS_UNUSED)
1083 {
1084 disconnect_remote_server();
1085 connect_to_remote_server = false;
1086 unixctl_command_reply(conn, NULL);
1087 }
1088
1089 static void
1090 ovsdb_server_set_sync_excluded_tables(struct unixctl_conn *conn,
1091 int argc OVS_UNUSED,
1092 const char *argv[],
1093 void *arg_ OVS_UNUSED)
1094 {
1095 set_tables_blacklist(argv[1]);
1096 unixctl_command_reply(conn, NULL);
1097 }
1098
1099 static void
1100 ovsdb_server_get_sync_excluded_tables(struct unixctl_conn *conn,
1101 int argc OVS_UNUSED,
1102 const char *argv[] OVS_UNUSED,
1103 void *arg_ OVS_UNUSED)
1104 {
1105 struct ds s;
1106 const char *table_name;
1107 struct sset table_blacklist = get_tables_blacklist();
1108
1109 ds_init(&s);
1110
1111 SSET_FOR_EACH(table_name, &table_blacklist) {
1112 ds_put_format(&s, "%s\n", table_name);
1113 }
1114
1115 unixctl_command_reply(conn, ds_cstr(&s));
1116 }
1117
1118 static void
1119 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
1120 const char *argv[] OVS_UNUSED,
1121 void *exiting_)
1122 {
1123 bool *exiting = exiting_;
1124 *exiting = true;
1125 unixctl_command_reply(conn, NULL);
1126 }
1127
1128 static void
1129 ovsdb_server_perf_counters_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1130 const char *argv[] OVS_UNUSED,
1131 void *arg_ OVS_UNUSED)
1132 {
1133 char *s = perf_counters_to_string();
1134
1135 unixctl_command_reply(conn, s);
1136 free(s);
1137 }
1138
1139 static void
1140 ovsdb_server_perf_counters_clear(struct unixctl_conn *conn, int argc OVS_UNUSED,
1141 const char *argv[] OVS_UNUSED,
1142 void *arg_ OVS_UNUSED)
1143 {
1144 perf_counters_clear();
1145 unixctl_command_reply(conn, NULL);
1146 }
1147
1148 /* "ovsdb-server/disable-monitor-cond": makes ovsdb-server drop all of its
1149 * JSON-RPC connections and reconnect. New sessions will not recognize
1150 * the 'monitor_cond' method. */
1151 static void
1152 ovsdb_server_disable_monitor_cond(struct unixctl_conn *conn,
1153 int argc OVS_UNUSED,
1154 const char *argv[] OVS_UNUSED,
1155 void *jsonrpc_)
1156 {
1157 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1158
1159 ovsdb_jsonrpc_disable_monitor_cond();
1160 ovsdb_jsonrpc_server_reconnect(jsonrpc);
1161 unixctl_command_reply(conn, NULL);
1162 }
1163
1164 static void
1165 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
1166 const char *argv[], void *dbs_)
1167 {
1168 struct shash *all_dbs = dbs_;
1169 struct ds reply;
1170 struct db *db;
1171 struct shash_node *node;
1172 int n = 0;
1173
1174 ds_init(&reply);
1175 SHASH_FOR_EACH(node, all_dbs) {
1176 const char *name;
1177
1178 db = node->data;
1179 name = db->db->schema->name;
1180
1181 if (argc < 2 || !strcmp(argv[1], name)) {
1182 struct ovsdb_error *error;
1183
1184 VLOG_INFO("compacting %s database by user request", name);
1185
1186 error = ovsdb_file_compact(db->file);
1187 if (error) {
1188 char *s = ovsdb_error_to_string(error);
1189 ds_put_format(&reply, "%s\n", s);
1190 free(s);
1191 ovsdb_error_destroy(error);
1192 }
1193
1194 n++;
1195 }
1196 }
1197
1198 if (!n) {
1199 unixctl_command_reply_error(conn, "no database by that name");
1200 } else if (reply.length) {
1201 unixctl_command_reply_error(conn, ds_cstr(&reply));
1202 } else {
1203 unixctl_command_reply(conn, NULL);
1204 }
1205 ds_destroy(&reply);
1206 }
1207
1208 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1209 * connections and reconnect. */
1210 static void
1211 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
1212 const char *argv[] OVS_UNUSED, void *jsonrpc_)
1213 {
1214 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1215
1216 ovsdb_jsonrpc_server_reconnect(jsonrpc);
1217 unixctl_command_reply(conn, NULL);
1218 }
1219
1220 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1221 * ovsdb-server services. */
1222 static void
1223 ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1224 const char *argv[], void *config_)
1225 {
1226 struct server_config *config = config_;
1227 const char *remote = argv[1];
1228
1229 const struct ovsdb_column *column;
1230 const struct ovsdb_table *table;
1231 const struct db *db;
1232 char *retval;
1233
1234 retval = (strncmp("db:", remote, 3)
1235 ? NULL
1236 : parse_db_column(config->all_dbs, remote,
1237 &db, &table, &column));
1238 if (!retval) {
1239 if (sset_add(config->remotes, remote)) {
1240 save_config(config);
1241 }
1242 unixctl_command_reply(conn, NULL);
1243 } else {
1244 unixctl_command_reply_error(conn, retval);
1245 free(retval);
1246 }
1247 }
1248
1249 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1250 * that ovsdb-server services. */
1251 static void
1252 ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1253 const char *argv[], void *config_)
1254 {
1255 struct server_config *config = config_;
1256 struct sset_node *node;
1257
1258 node = sset_find(config->remotes, argv[1]);
1259 if (node) {
1260 sset_delete(config->remotes, node);
1261 save_config(config);
1262 unixctl_command_reply(conn, NULL);
1263 } else {
1264 unixctl_command_reply_error(conn, "no such remote");
1265 }
1266 }
1267
1268 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1269 static void
1270 ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
1271 const char *argv[] OVS_UNUSED, void *remotes_)
1272 {
1273 struct sset *remotes = remotes_;
1274 const char **list, **p;
1275 struct ds s;
1276
1277 ds_init(&s);
1278
1279 list = sset_sort(remotes);
1280 for (p = list; *p; p++) {
1281 ds_put_format(&s, "%s\n", *p);
1282 }
1283 free(list);
1284
1285 unixctl_command_reply(conn, ds_cstr(&s));
1286 ds_destroy(&s);
1287 }
1288
1289
1290 /* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1291 static void
1292 ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1293 const char *argv[], void *config_)
1294 {
1295 struct server_config *config = config_;
1296 const char *filename = argv[1];
1297 char *error;
1298
1299 error = open_db(config, filename);
1300 if (!error) {
1301 save_config(config);
1302 unixctl_command_reply(conn, NULL);
1303 } else {
1304 unixctl_command_reply_error(conn, error);
1305 free(error);
1306 }
1307 }
1308
1309 static void
1310 ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1311 const char *argv[], void *config_)
1312 {
1313 struct server_config *config = config_;
1314 struct shash_node *node;
1315 struct db *db;
1316 bool ok;
1317
1318 node = shash_find(config->all_dbs, argv[1]);
1319 if (!node) {
1320 unixctl_command_reply_error(conn, "Failed to find the database.");
1321 return;
1322 }
1323 db = node->data;
1324
1325 ok = ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db);
1326 ovs_assert(ok);
1327
1328 close_db(db);
1329 shash_delete(config->all_dbs, node);
1330
1331 save_config(config);
1332 unixctl_command_reply(conn, NULL);
1333 }
1334
1335 static void
1336 ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
1337 const char *argv[] OVS_UNUSED, void *all_dbs_)
1338 {
1339 struct shash *all_dbs = all_dbs_;
1340 const struct shash_node **nodes;
1341 struct ds s;
1342 size_t i;
1343
1344 ds_init(&s);
1345
1346 nodes = shash_sort(all_dbs);
1347 for (i = 0; i < shash_count(all_dbs); i++) {
1348 struct db *db = nodes[i]->data;
1349 ds_put_format(&s, "%s\n", db->db->schema->name);
1350 }
1351 free(nodes);
1352
1353 unixctl_command_reply(conn, ds_cstr(&s));
1354 ds_destroy(&s);
1355 }
1356
1357 static void
1358 parse_options(int *argcp, char **argvp[],
1359 struct sset *remotes, char **unixctl_pathp, char **run_command)
1360 {
1361 enum {
1362 OPT_REMOTE = UCHAR_MAX + 1,
1363 OPT_UNIXCTL,
1364 OPT_RUN,
1365 OPT_BOOTSTRAP_CA_CERT,
1366 OPT_PEER_CA_CERT,
1367 OPT_SYNC_FROM,
1368 OPT_SYNC_EXCLUDE,
1369 VLOG_OPTION_ENUMS,
1370 DAEMON_OPTION_ENUMS
1371 };
1372 static const struct option long_options[] = {
1373 {"remote", required_argument, NULL, OPT_REMOTE},
1374 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
1375 #ifndef _WIN32
1376 {"run", required_argument, NULL, OPT_RUN},
1377 #endif
1378 {"help", no_argument, NULL, 'h'},
1379 {"version", no_argument, NULL, 'V'},
1380 DAEMON_LONG_OPTIONS,
1381 VLOG_LONG_OPTIONS,
1382 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
1383 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
1384 {"private-key", required_argument, NULL, 'p'},
1385 {"certificate", required_argument, NULL, 'c'},
1386 {"ca-cert", required_argument, NULL, 'C'},
1387 {"sync-from", required_argument, NULL, OPT_SYNC_FROM},
1388 {"sync-exclude-tables", required_argument, NULL, OPT_SYNC_EXCLUDE},
1389 {NULL, 0, NULL, 0},
1390 };
1391 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
1392 int argc = *argcp;
1393 char **argv = *argvp;
1394
1395 sset_init(remotes);
1396 for (;;) {
1397 int c;
1398
1399 c = getopt_long(argc, argv, short_options, long_options, NULL);
1400 if (c == -1) {
1401 break;
1402 }
1403
1404 switch (c) {
1405 case OPT_REMOTE:
1406 sset_add(remotes, optarg);
1407 break;
1408
1409 case OPT_UNIXCTL:
1410 *unixctl_pathp = optarg;
1411 break;
1412
1413 case OPT_RUN:
1414 *run_command = optarg;
1415 break;
1416
1417 case 'h':
1418 usage();
1419
1420 case 'V':
1421 ovs_print_version(0, 0);
1422 exit(EXIT_SUCCESS);
1423
1424 VLOG_OPTION_HANDLERS
1425 DAEMON_OPTION_HANDLERS
1426
1427 case 'p':
1428 private_key_file = optarg;
1429 break;
1430
1431 case 'c':
1432 certificate_file = optarg;
1433 break;
1434
1435 case 'C':
1436 ca_cert_file = optarg;
1437 bootstrap_ca_cert = false;
1438 break;
1439
1440 case OPT_BOOTSTRAP_CA_CERT:
1441 ca_cert_file = optarg;
1442 bootstrap_ca_cert = true;
1443 break;
1444
1445 case OPT_PEER_CA_CERT:
1446 stream_ssl_set_peer_ca_cert_file(optarg);
1447 break;
1448
1449 case OPT_SYNC_FROM:
1450 set_remote_ovsdb_server(optarg);
1451 connect_to_remote_server = true;
1452 break;
1453
1454 case OPT_SYNC_EXCLUDE:
1455 set_tables_blacklist(optarg);
1456 break;
1457
1458 case '?':
1459 exit(EXIT_FAILURE);
1460
1461 default:
1462 abort();
1463 }
1464 }
1465 free(short_options);
1466
1467 *argcp -= optind;
1468 *argvp += optind;
1469 }
1470
1471 static void
1472 usage(void)
1473 {
1474 printf("%s: Open vSwitch database server\n"
1475 "usage: %s [OPTIONS] [DATABASE...]\n"
1476 "where each DATABASE is a database file in ovsdb format.\n"
1477 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1478 program_name, program_name, ovs_dbdir());
1479 printf("\nJSON-RPC options (may be specified any number of times):\n"
1480 " --remote=REMOTE connect or listen to REMOTE\n");
1481 stream_usage("JSON-RPC", true, true, true);
1482 daemon_usage();
1483 vlog_usage();
1484 replication_usage();
1485 printf("\nOther options:\n"
1486 " --run COMMAND run COMMAND as subprocess then exit\n"
1487 " --unixctl=SOCKET override default control socket name\n"
1488 " -h, --help display this help message\n"
1489 " -V, --version display version information\n");
1490 exit(EXIT_SUCCESS);
1491 }
1492 \f
1493 static struct json *
1494 sset_to_json(const struct sset *sset)
1495 {
1496 struct json *array;
1497 const char *s;
1498
1499 array = json_array_create_empty();
1500 SSET_FOR_EACH (s, sset) {
1501 json_array_add(array, json_string_create(s));
1502 }
1503 return array;
1504 }
1505
1506 /* Truncates and replaces the contents of 'config_file' by a representation of
1507 * 'remotes' and 'db_filenames'. */
1508 static void
1509 save_config__(FILE *config_file, const struct sset *remotes,
1510 const struct sset *db_filenames)
1511 {
1512 struct json *obj;
1513 char *s;
1514
1515 if (ftruncate(fileno(config_file), 0) == -1) {
1516 VLOG_FATAL("failed to truncate temporary file (%s)",
1517 ovs_strerror(errno));
1518 }
1519
1520 obj = json_object_create();
1521 json_object_put(obj, "remotes", sset_to_json(remotes));
1522 json_object_put(obj, "db_filenames", sset_to_json(db_filenames));
1523 s = json_to_string(obj, 0);
1524 json_destroy(obj);
1525
1526 if (fseek(config_file, 0, SEEK_SET) != 0
1527 || fputs(s, config_file) == EOF
1528 || fflush(config_file) == EOF) {
1529 VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno));
1530 }
1531 free(s);
1532 }
1533
1534 /* Truncates and replaces the contents of 'config_file' by a representation of
1535 * 'config'. */
1536 static void
1537 save_config(struct server_config *config)
1538 {
1539 struct sset db_filenames;
1540 struct shash_node *node;
1541
1542 sset_init(&db_filenames);
1543 SHASH_FOR_EACH (node, config->all_dbs) {
1544 struct db *db = node->data;
1545 sset_add(&db_filenames, db->filename);
1546 }
1547
1548 save_config__(config->config_tmpfile, config->remotes, &db_filenames);
1549
1550 sset_destroy(&db_filenames);
1551 }
1552
1553 static void
1554 sset_from_json(struct sset *sset, const struct json *array)
1555 {
1556 size_t i;
1557
1558 sset_clear(sset);
1559
1560 ovs_assert(array->type == JSON_ARRAY);
1561 for (i = 0; i < array->u.array.n; i++) {
1562 const struct json *elem = array->u.array.elems[i];
1563 sset_add(sset, json_string(elem));
1564 }
1565 }
1566
1567 /* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
1568 * 'config_file', which must have been previously written by save_config(). */
1569 static void
1570 load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames)
1571 {
1572 struct json *json;
1573
1574 if (fseek(config_file, 0, SEEK_SET) != 0) {
1575 VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno));
1576 }
1577 json = json_from_stream(config_file);
1578 if (json->type == JSON_STRING) {
1579 VLOG_FATAL("reading json failed (%s)", json_string(json));
1580 }
1581 ovs_assert(json->type == JSON_OBJECT);
1582
1583 sset_from_json(remotes, shash_find_data(json_object(json), "remotes"));
1584 sset_from_json(db_filenames,
1585 shash_find_data(json_object(json), "db_filenames"));
1586 json_destroy(json);
1587 }