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