]> git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/ovsdb-server.c
raft: Avoid annoying debug logs if raft is connected.
[mirror_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 "openvswitch/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 "storage.h"
53 #include "table.h"
54 #include "timeval.h"
55 #include "transaction.h"
56 #include "trigger.h"
57 #include "util.h"
58 #include "unixctl.h"
59 #include "perf-counter.h"
60 #include "ovsdb-util.h"
61 #include "openvswitch/vlog.h"
62
63 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
64
65 struct db {
66 char *filename;
67 struct ovsdb *db;
68 struct uuid row_uuid;
69 };
70
71 /* SSL configuration. */
72 static char *private_key_file;
73 static char *certificate_file;
74 static char *ca_cert_file;
75 static char *ssl_protocols;
76 static char *ssl_ciphers;
77 static bool bootstrap_ca_cert;
78
79 static unixctl_cb_func ovsdb_server_exit;
80 static unixctl_cb_func ovsdb_server_compact;
81 static unixctl_cb_func ovsdb_server_reconnect;
82 static unixctl_cb_func ovsdb_server_perf_counters_clear;
83 static unixctl_cb_func ovsdb_server_perf_counters_show;
84 static unixctl_cb_func ovsdb_server_disable_monitor_cond;
85 static unixctl_cb_func ovsdb_server_set_active_ovsdb_server;
86 static unixctl_cb_func ovsdb_server_get_active_ovsdb_server;
87 static unixctl_cb_func ovsdb_server_connect_active_ovsdb_server;
88 static unixctl_cb_func ovsdb_server_disconnect_active_ovsdb_server;
89 static unixctl_cb_func ovsdb_server_set_active_ovsdb_server_probe_interval;
90 static unixctl_cb_func ovsdb_server_set_sync_exclude_tables;
91 static unixctl_cb_func ovsdb_server_get_sync_exclude_tables;
92 static unixctl_cb_func ovsdb_server_get_sync_status;
93 static unixctl_cb_func ovsdb_server_get_db_storage_status;
94
95 struct server_config {
96 struct sset *remotes;
97 struct shash *all_dbs;
98 FILE *config_tmpfile;
99 char **sync_from;
100 char **sync_exclude;
101 bool *is_backup;
102 int *replication_probe_interval;
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 void read_db(struct server_config *, struct db *);
114 static struct ovsdb_error *open_db(struct server_config *,
115 const char *filename)
116 OVS_WARN_UNUSED_RESULT;
117 static void add_server_db(struct server_config *);
118 static void remove_db(struct server_config *, struct shash_node *db, char *);
119 static void close_db(struct server_config *, struct db *, char *);
120
121 static void parse_options(int argc, char *argvp[],
122 struct sset *db_filenames, struct sset *remotes,
123 char **unixctl_pathp, char **run_command,
124 char **sync_from, char **sync_exclude,
125 bool *is_backup);
126 OVS_NO_RETURN static void usage(void);
127
128 static char *reconfigure_remotes(struct ovsdb_jsonrpc_server *,
129 const struct shash *all_dbs,
130 struct sset *remotes);
131 static char *reconfigure_ssl(const struct shash *all_dbs);
132 static void report_error_if_changed(char *error, char **last_errorp);
133
134 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
135 const struct sset *remotes,
136 struct shash *all_dbs);
137 static void update_server_status(struct shash *all_dbs);
138
139 static void save_config__(FILE *config_file, const struct sset *remotes,
140 const struct sset *db_filenames,
141 const char *sync_from, const char *sync_exclude,
142 bool is_backup);
143 static void save_config(struct server_config *);
144 static void load_config(FILE *config_file, struct sset *remotes,
145 struct sset *db_filenames, char **sync_from,
146 char **sync_exclude, bool *is_backup);
147
148 static void
149 ovsdb_replication_init(const char *sync_from, const char *exclude,
150 struct shash *all_dbs, const struct uuid *server_uuid,
151 int probe_interval)
152 {
153 replication_init(sync_from, exclude, server_uuid, probe_interval);
154 struct shash_node *node;
155 SHASH_FOR_EACH (node, all_dbs) {
156 struct db *db = node->data;
157 if (node->name[0] != '_' && db->db) {
158 replication_add_local_db(node->name, db->db);
159 }
160 }
161 }
162
163 static void
164 log_and_free_error(struct ovsdb_error *error)
165 {
166 if (error) {
167 char *s = ovsdb_error_to_string_free(error);
168 VLOG_INFO("%s", s);
169 free(s);
170 }
171 }
172
173 static void
174 main_loop(struct server_config *config,
175 struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs,
176 struct unixctl_server *unixctl, struct sset *remotes,
177 struct process *run_process, bool *exiting, bool *is_backup)
178 {
179 char *remotes_error, *ssl_error;
180 struct shash_node *node;
181 long long int status_timer = LLONG_MIN;
182
183 *exiting = false;
184 ssl_error = NULL;
185 remotes_error = NULL;
186 while (!*exiting) {
187 memory_run();
188 if (memory_should_report()) {
189 struct simap usage;
190
191 simap_init(&usage);
192 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
193 ovsdb_monitor_get_memory_usage(&usage);
194 SHASH_FOR_EACH(node, all_dbs) {
195 struct db *db = node->data;
196 ovsdb_get_memory_usage(db->db, &usage);
197 }
198 memory_report(&usage);
199 simap_destroy(&usage);
200 }
201
202 /* Run unixctl_server_run() before reconfigure_remotes() because
203 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
204 * the set of remotes that reconfigure_remotes() uses. */
205 unixctl_server_run(unixctl);
206
207 ovsdb_jsonrpc_server_set_read_only(jsonrpc, *is_backup);
208
209 report_error_if_changed(
210 reconfigure_remotes(jsonrpc, all_dbs, remotes),
211 &remotes_error);
212 report_error_if_changed(reconfigure_ssl(all_dbs), &ssl_error);
213 ovsdb_jsonrpc_server_run(jsonrpc);
214
215 if (*is_backup) {
216 replication_run();
217 if (!replication_is_alive()) {
218 disconnect_active_server();
219 *is_backup = false;
220 }
221 }
222
223 struct shash_node *next;
224 SHASH_FOR_EACH_SAFE (node, next, all_dbs) {
225 struct db *db = node->data;
226 ovsdb_txn_history_run(db->db);
227 ovsdb_storage_run(db->db->storage);
228 read_db(config, db);
229 /* Run triggers after storage_run and read_db to make sure new raft
230 * updates are utilized in current iteration. */
231 if (ovsdb_trigger_run(db->db, time_msec())) {
232 /* The message below is currently the only reason to disconnect
233 * all clients. */
234 ovsdb_jsonrpc_server_reconnect(
235 jsonrpc, false,
236 xasprintf("committed %s database schema conversion",
237 db->db->name));
238 }
239 if (ovsdb_storage_is_dead(db->db->storage)) {
240 VLOG_INFO("%s: removing database because storage disconnected "
241 "permanently", node->name);
242 remove_db(config, node,
243 xasprintf("removing database %s because storage "
244 "disconnected permanently", node->name));
245 } else if (ovsdb_storage_should_snapshot(db->db->storage)) {
246 log_and_free_error(ovsdb_snapshot(db->db));
247 }
248 }
249 if (run_process) {
250 process_run();
251 if (process_exited(run_process)) {
252 *exiting = true;
253 }
254 }
255
256 /* update Manager status(es) every 2.5 seconds */
257 if (time_msec() >= status_timer) {
258 status_timer = time_msec() + 2500;
259 update_remote_status(jsonrpc, remotes, all_dbs);
260 }
261
262 update_server_status(all_dbs);
263
264 memory_wait();
265 if (*is_backup) {
266 replication_wait();
267 }
268
269 ovsdb_jsonrpc_server_wait(jsonrpc);
270 unixctl_server_wait(unixctl);
271 SHASH_FOR_EACH(node, all_dbs) {
272 struct db *db = node->data;
273 ovsdb_trigger_wait(db->db, time_msec());
274 ovsdb_storage_wait(db->db->storage);
275 ovsdb_storage_read_wait(db->db->storage);
276 }
277 if (run_process) {
278 process_wait(run_process);
279 }
280 if (*exiting) {
281 poll_immediate_wake();
282 }
283 poll_timer_wait_until(status_timer);
284 poll_block();
285 if (should_service_stop()) {
286 *exiting = true;
287 }
288 }
289
290 free(remotes_error);
291 }
292
293 int
294 main(int argc, char *argv[])
295 {
296 char *unixctl_path = NULL;
297 char *run_command = NULL;
298 struct unixctl_server *unixctl;
299 struct ovsdb_jsonrpc_server *jsonrpc;
300 struct sset remotes, db_filenames;
301 char *sync_from, *sync_exclude;
302 bool is_backup;
303 const char *db_filename;
304 struct process *run_process;
305 bool exiting;
306 int retval;
307 FILE *config_tmpfile;
308 struct server_config server_config;
309 struct shash all_dbs;
310 struct shash_node *node, *next;
311 int replication_probe_interval = REPLICATION_DEFAULT_PROBE_INTERVAL;
312
313 ovs_cmdl_proctitle_init(argc, argv);
314 set_program_name(argv[0]);
315 service_start(&argc, &argv);
316 fatal_ignore_sigpipe();
317 process_init();
318
319 bool active = false;
320 parse_options(argc, argv, &db_filenames, &remotes, &unixctl_path,
321 &run_command, &sync_from, &sync_exclude, &active);
322 is_backup = sync_from && !active;
323
324 daemon_become_new_user(false);
325
326 /* Create and initialize 'config_tmpfile' as a temporary file to hold
327 * ovsdb-server's most basic configuration, and then save our initial
328 * configuration to it. When --monitor is used, this preserves the effects
329 * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
330 * new configuration) across crashes. */
331 config_tmpfile = tmpfile();
332 if (!config_tmpfile) {
333 ovs_fatal(errno, "failed to create temporary file");
334 }
335
336 server_config.remotes = &remotes;
337 server_config.config_tmpfile = config_tmpfile;
338
339 save_config__(config_tmpfile, &remotes, &db_filenames, sync_from,
340 sync_exclude, is_backup);
341
342 daemonize_start(false);
343
344 /* Load the saved config. */
345 load_config(config_tmpfile, &remotes, &db_filenames, &sync_from,
346 &sync_exclude, &is_backup);
347
348 /* Start ovsdb jsonrpc server. When running as a backup server,
349 * jsonrpc connections are read only. Otherwise, both read
350 * and write transactions are allowed. */
351 jsonrpc = ovsdb_jsonrpc_server_create(is_backup);
352
353 shash_init(&all_dbs);
354 server_config.all_dbs = &all_dbs;
355 server_config.jsonrpc = jsonrpc;
356 server_config.sync_from = &sync_from;
357 server_config.sync_exclude = &sync_exclude;
358 server_config.is_backup = &is_backup;
359 server_config.replication_probe_interval = &replication_probe_interval;
360
361 perf_counters_init();
362
363 SSET_FOR_EACH (db_filename, &db_filenames) {
364 struct ovsdb_error *error = open_db(&server_config, db_filename);
365 if (error) {
366 char *s = ovsdb_error_to_string_free(error);
367 ovs_fatal(0, "%s", s);
368 }
369 }
370 add_server_db(&server_config);
371
372 char *error = reconfigure_remotes(jsonrpc, &all_dbs, &remotes);
373 if (!error) {
374 error = reconfigure_ssl(&all_dbs);
375 }
376 if (error) {
377 ovs_fatal(0, "%s", error);
378 }
379
380 retval = unixctl_server_create(unixctl_path, &unixctl);
381 if (retval) {
382 exit(EXIT_FAILURE);
383 }
384
385 if (run_command) {
386 char *run_argv[4];
387
388 run_argv[0] = "/bin/sh";
389 run_argv[1] = "-c";
390 run_argv[2] = run_command;
391 run_argv[3] = NULL;
392
393 retval = process_start(run_argv, &run_process);
394 if (retval) {
395 ovs_fatal(retval, "%s: process failed to start", run_command);
396 }
397 } else {
398 run_process = NULL;
399 }
400
401 daemonize_complete();
402
403 if (!run_command) {
404 /* ovsdb-server is usually a long-running process, in which case it
405 * makes plenty of sense to log the version, but --run makes
406 * ovsdb-server more like a command-line tool, so skip it. */
407 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
408 }
409
410 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
411 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
412 ovsdb_server_compact, &all_dbs);
413 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
414 ovsdb_server_reconnect, jsonrpc);
415
416 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
417 ovsdb_server_add_remote, &server_config);
418 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
419 ovsdb_server_remove_remote, &server_config);
420 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
421 ovsdb_server_list_remotes, &remotes);
422
423 unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
424 ovsdb_server_add_database, &server_config);
425 unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
426 ovsdb_server_remove_database, &server_config);
427 unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
428 ovsdb_server_list_databases, &all_dbs);
429 unixctl_command_register("ovsdb-server/perf-counters-show", "", 0, 0,
430 ovsdb_server_perf_counters_show, NULL);
431 unixctl_command_register("ovsdb-server/perf-counters-clear", "", 0, 0,
432 ovsdb_server_perf_counters_clear, NULL);
433 unixctl_command_register("ovsdb-server/set-active-ovsdb-server", "", 1, 1,
434 ovsdb_server_set_active_ovsdb_server,
435 &server_config);
436 unixctl_command_register("ovsdb-server/get-active-ovsdb-server", "", 0, 0,
437 ovsdb_server_get_active_ovsdb_server,
438 &server_config);
439 unixctl_command_register("ovsdb-server/connect-active-ovsdb-server", "",
440 0, 0, ovsdb_server_connect_active_ovsdb_server,
441 &server_config);
442 unixctl_command_register("ovsdb-server/disconnect-active-ovsdb-server", "",
443 0, 0, ovsdb_server_disconnect_active_ovsdb_server,
444 &server_config);
445 unixctl_command_register(
446 "ovsdb-server/set-active-ovsdb-server-probe-interval", "", 1, 1,
447 ovsdb_server_set_active_ovsdb_server_probe_interval, &server_config);
448 unixctl_command_register("ovsdb-server/set-sync-exclude-tables", "",
449 0, 1, ovsdb_server_set_sync_exclude_tables,
450 &server_config);
451 unixctl_command_register("ovsdb-server/get-sync-exclude-tables", "",
452 0, 0, ovsdb_server_get_sync_exclude_tables,
453 NULL);
454 unixctl_command_register("ovsdb-server/sync-status", "",
455 0, 0, ovsdb_server_get_sync_status,
456 &server_config);
457 unixctl_command_register("ovsdb-server/get-db-storage-status", "DB", 1, 1,
458 ovsdb_server_get_db_storage_status,
459 &server_config);
460
461 /* Simulate the behavior of OVS release prior to version 2.5 that
462 * does not support the monitor_cond method. */
463 unixctl_command_register("ovsdb-server/disable-monitor-cond", "", 0, 0,
464 ovsdb_server_disable_monitor_cond, jsonrpc);
465
466 if (is_backup) {
467 const struct uuid *server_uuid;
468 server_uuid = ovsdb_jsonrpc_server_get_uuid(jsonrpc);
469 ovsdb_replication_init(sync_from, sync_exclude, &all_dbs, server_uuid,
470 replication_probe_interval);
471 }
472
473 main_loop(&server_config, jsonrpc, &all_dbs, unixctl, &remotes,
474 run_process, &exiting, &is_backup);
475
476 SHASH_FOR_EACH_SAFE(node, next, &all_dbs) {
477 struct db *db = node->data;
478 close_db(&server_config, db, NULL);
479 shash_delete(&all_dbs, node);
480 }
481 ovsdb_jsonrpc_server_destroy(jsonrpc);
482 shash_destroy(&all_dbs);
483 sset_destroy(&remotes);
484 sset_destroy(&db_filenames);
485 free(sync_from);
486 free(sync_exclude);
487 unixctl_server_destroy(unixctl);
488 replication_destroy();
489
490 if (run_process && process_exited(run_process)) {
491 int status = process_status(run_process);
492 if (status) {
493 ovs_fatal(0, "%s: child exited, %s",
494 run_command, process_status_msg(status));
495 }
496 }
497 perf_counters_destroy();
498 service_stop();
499 return 0;
500 }
501
502 /* Returns true if 'filename' is known to be already open as a database,
503 * false if not.
504 *
505 * "False negatives" are possible. */
506 static bool
507 is_already_open(struct server_config *config OVS_UNUSED,
508 const char *filename OVS_UNUSED)
509 {
510 #ifndef _WIN32
511 struct stat s;
512
513 if (!stat(filename, &s)) {
514 struct shash_node *node;
515
516 SHASH_FOR_EACH (node, config->all_dbs) {
517 struct db *db = node->data;
518 struct stat s2;
519
520 if (!stat(db->filename, &s2)
521 && s.st_dev == s2.st_dev
522 && s.st_ino == s2.st_ino) {
523 return true;
524 }
525 }
526 }
527 #endif /* !_WIN32 */
528
529 return false;
530 }
531
532 static void
533 close_db(struct server_config *config, struct db *db, char *comment)
534 {
535 if (db) {
536 ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db, comment);
537 ovsdb_destroy(db->db);
538 free(db->filename);
539 free(db);
540 } else {
541 free(comment);
542 }
543 }
544
545 static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
546 parse_txn(struct server_config *config, struct db *db,
547 const struct ovsdb_schema *schema, const struct json *txn_json,
548 const struct uuid *txnid)
549 {
550 if (schema) {
551 /* We're replacing the schema (and the data). Destroy the database
552 * (first grabbing its storage), then replace it with the new schema.
553 * The transaction must also include the replacement data.
554 *
555 * Only clustered database schema changes and snapshot installs
556 * go through this path.
557 */
558 ovs_assert(txn_json);
559 ovs_assert(ovsdb_storage_is_clustered(db->db->storage));
560
561 struct ovsdb_error *error = ovsdb_schema_check_for_ephemeral_columns(
562 schema);
563 if (error) {
564 return error;
565 }
566
567 if (!db->db->schema ||
568 strcmp(schema->version, db->db->schema->version)) {
569 ovsdb_jsonrpc_server_reconnect(
570 config->jsonrpc, false,
571 (db->db->schema
572 ? xasprintf("database %s schema changed", db->db->name)
573 : xasprintf("database %s connected to storage",
574 db->db->name)));
575 }
576
577 ovsdb_replace(db->db, ovsdb_create(ovsdb_schema_clone(schema), NULL));
578
579 /* Force update to schema in _Server database. */
580 db->row_uuid = UUID_ZERO;
581 }
582
583 if (txn_json) {
584 if (!db->db->schema) {
585 return ovsdb_error(NULL, "%s: data without schema", db->filename);
586 }
587
588 struct ovsdb_txn *txn;
589 struct ovsdb_error *error;
590
591 error = ovsdb_file_txn_from_json(db->db, txn_json, false, &txn);
592 if (!error) {
593 ovsdb_txn_set_txnid(txnid, txn);
594 log_and_free_error(ovsdb_txn_replay_commit(txn));
595 }
596 if (!error && !uuid_is_zero(txnid)) {
597 db->db->prereq = *txnid;
598 }
599 if (error) {
600 ovsdb_storage_unread(db->db->storage);
601 return error;
602 }
603 }
604
605 return NULL;
606 }
607
608 static void
609 read_db(struct server_config *config, struct db *db)
610 {
611 struct ovsdb_error *error;
612 for (;;) {
613 struct ovsdb_schema *schema;
614 struct json *txn_json;
615 struct uuid txnid;
616 error = ovsdb_storage_read(db->db->storage, &schema, &txn_json,
617 &txnid);
618 if (error) {
619 break;
620 } else if (!schema && !txn_json) {
621 /* End of file. */
622 return;
623 } else {
624 error = parse_txn(config, db, schema, txn_json, &txnid);
625 json_destroy(txn_json);
626 ovsdb_schema_destroy(schema);
627 if (error) {
628 break;
629 }
630 }
631 }
632
633 /* Log error but otherwise ignore it. Probably the database just
634 * got truncated due to power failure etc. and we should use its
635 * current contents. */
636 char *msg = ovsdb_error_to_string_free(error);
637 VLOG_ERR("%s", msg);
638 free(msg);
639 }
640
641 static void
642 add_db(struct server_config *config, struct db *db)
643 {
644 db->row_uuid = UUID_ZERO;
645 shash_add_assert(config->all_dbs, db->db->name, db);
646 }
647
648 static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
649 open_db(struct server_config *config, const char *filename)
650 {
651 struct db *db;
652
653 /* If we know that the file is already open, return a good error message.
654 * Otherwise, if the file is open, we'll fail later on with a harder to
655 * interpret file locking error. */
656 if (is_already_open(config, filename)) {
657 return ovsdb_error(NULL, "%s: already open", filename);
658 }
659
660 struct ovsdb_storage *storage;
661 struct ovsdb_error *error;
662 error = ovsdb_storage_open(filename, true, &storage);
663 if (error) {
664 return error;
665 }
666
667 db = xzalloc(sizeof *db);
668 db->filename = xstrdup(filename);
669
670 struct ovsdb_schema *schema;
671 if (ovsdb_storage_is_clustered(storage)) {
672 schema = NULL;
673 } else {
674 struct json *txn_json;
675 error = ovsdb_storage_read(storage, &schema, &txn_json, NULL);
676 if (error) {
677 ovsdb_storage_close(storage);
678 return error;
679 }
680 ovs_assert(schema && !txn_json);
681 }
682 db->db = ovsdb_create(schema, storage);
683 ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db);
684
685 /* Enable txn history for clustered mode. It is not enabled for other mode
686 * for now, since txn id is available for clustered mode only. */
687 ovsdb_txn_history_init(db->db, ovsdb_storage_is_clustered(storage));
688
689 read_db(config, db);
690
691 error = (db->db->name[0] == '_'
692 ? ovsdb_error(NULL, "%s: names beginning with \"_\" are reserved",
693 db->db->name)
694 : shash_find(config->all_dbs, db->db->name)
695 ? ovsdb_error(NULL, "%s: duplicate database name", db->db->name)
696 : NULL);
697 if (error) {
698 char *error_s = ovsdb_error_to_string(error);
699 close_db(config, db,
700 xasprintf("cannot complete opening %s database (%s)",
701 db->db->name, error_s));
702 free(error_s);
703 return error;
704 }
705
706 add_db(config, db);
707 return NULL;
708 }
709
710 /* Add the internal _Server database to the server configuration. */
711 static void
712 add_server_db(struct server_config *config)
713 {
714 struct json *schema_json = json_from_string(
715 #include "ovsdb/_server.ovsschema.inc"
716 );
717 ovs_assert(schema_json->type == JSON_OBJECT);
718
719 struct ovsdb_schema *schema;
720 struct ovsdb_error *error OVS_UNUSED = ovsdb_schema_from_json(schema_json,
721 &schema);
722 ovs_assert(!error);
723 json_destroy(schema_json);
724
725 struct db *db = xzalloc(sizeof *db);
726 /* We don't need txn_history for server_db. */
727
728 db->filename = xstrdup("<internal>");
729 db->db = ovsdb_create(schema, ovsdb_storage_create_unbacked());
730 bool ok OVS_UNUSED = ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db);
731 ovs_assert(ok);
732 add_db(config, db);
733 }
734
735 static char * OVS_WARN_UNUSED_RESULT
736 parse_db_column__(const struct shash *all_dbs,
737 const char *name_, char *name,
738 const struct db **dbp,
739 const struct ovsdb_table **tablep,
740 const struct ovsdb_column **columnp)
741 {
742 const char *db_name, *table_name, *column_name;
743 const char *tokens[3];
744 char *save_ptr = NULL;
745
746 *dbp = NULL;
747 *tablep = NULL;
748 *columnp = NULL;
749
750 strtok_r(name, ":", &save_ptr); /* "db:" */
751 tokens[0] = strtok_r(NULL, ",", &save_ptr);
752 tokens[1] = strtok_r(NULL, ",", &save_ptr);
753 tokens[2] = strtok_r(NULL, ",", &save_ptr);
754 if (!tokens[0] || !tokens[1] || !tokens[2]) {
755 return xasprintf("\"%s\": invalid syntax", name_);
756 }
757
758 db_name = tokens[0];
759 table_name = tokens[1];
760 column_name = tokens[2];
761
762 *dbp = shash_find_data(all_dbs, tokens[0]);
763 if (!*dbp) {
764 return xasprintf("\"%s\": no database named %s", name_, db_name);
765 }
766
767 *tablep = ovsdb_get_table((*dbp)->db, table_name);
768 if (!*tablep) {
769 return xasprintf("\"%s\": no table named %s", name_, table_name);
770 }
771
772 *columnp = ovsdb_table_schema_get_column((*tablep)->schema, column_name);
773 if (!*columnp) {
774 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
775 name_, table_name, column_name);
776 }
777
778 return NULL;
779 }
780
781 /* Returns NULL if successful, otherwise a malloc()'d string describing the
782 * error. */
783 static char * OVS_WARN_UNUSED_RESULT
784 parse_db_column(const struct shash *all_dbs,
785 const char *name_,
786 const struct db **dbp,
787 const struct ovsdb_table **tablep,
788 const struct ovsdb_column **columnp)
789 {
790 char *name = xstrdup(name_);
791 char *retval = parse_db_column__(all_dbs, name_, name,
792 dbp, tablep, columnp);
793 free(name);
794 return retval;
795 }
796
797 /* Returns NULL if successful, otherwise a malloc()'d string describing the
798 * error. */
799 static char * OVS_WARN_UNUSED_RESULT
800 parse_db_string_column(const struct shash *all_dbs,
801 const char *name,
802 const struct db **dbp,
803 const struct ovsdb_table **tablep,
804 const struct ovsdb_column **columnp)
805 {
806 char *retval;
807
808 retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
809 if (retval) {
810 return retval;
811 }
812
813 if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
814 || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
815 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
816 "not string or set of strings",
817 name, (*tablep)->schema->name, (*columnp)->name);
818 }
819
820 return NULL;
821 }
822
823 static const char *
824 query_db_string(const struct shash *all_dbs, const char *name,
825 struct ds *errors)
826 {
827 if (!name || strncmp(name, "db:", 3)) {
828 return name;
829 } else {
830 const struct ovsdb_column *column;
831 const struct ovsdb_table *table;
832 const struct ovsdb_row *row;
833 const struct db *db;
834 char *retval;
835
836 retval = parse_db_string_column(all_dbs, name,
837 &db, &table, &column);
838 if (retval) {
839 if (db && !db->db->schema) {
840 /* 'db' is a clustered database but it hasn't connected to the
841 * cluster yet, so we can't get anything out of it, not even a
842 * schema. Not really an error. */
843 } else {
844 ds_put_format(errors, "%s\n", retval);
845 }
846 free(retval);
847 return NULL;
848 }
849
850 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
851 const struct ovsdb_datum *datum;
852 size_t i;
853
854 datum = &row->fields[column->index];
855 for (i = 0; i < datum->n; i++) {
856 if (datum->keys[i].string[0]) {
857 return datum->keys[i].string;
858 }
859 }
860 }
861 return NULL;
862 }
863 }
864
865 static struct ovsdb_jsonrpc_options *
866 add_remote(struct shash *remotes, const char *target)
867 {
868 struct ovsdb_jsonrpc_options *options;
869
870 options = shash_find_data(remotes, target);
871 if (!options) {
872 options = ovsdb_jsonrpc_default_options(target);
873 shash_add(remotes, target, options);
874 }
875
876 return options;
877 }
878
879 static void
880 free_remotes(struct shash *remotes)
881 {
882 if (remotes) {
883 struct shash_node *node;
884
885 SHASH_FOR_EACH (node, remotes) {
886 struct ovsdb_jsonrpc_options *options = node->data;
887 free(options->role);
888 }
889 shash_destroy_free_data(remotes);
890 }
891 }
892
893 /* Adds a remote and options to 'remotes', based on the Manager table row in
894 * 'row'. */
895 static void
896 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
897 {
898 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
899 struct ovsdb_jsonrpc_options *options;
900 long long int max_backoff, probe_interval;
901 bool read_only;
902 const char *target, *dscp_string, *role;
903
904 if (!ovsdb_util_read_string_column(row, "target", &target) || !target) {
905 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
906 row->table->schema->name);
907 return;
908 }
909
910 options = add_remote(remotes, target);
911 if (ovsdb_util_read_integer_column(row, "max_backoff", &max_backoff)) {
912 options->max_backoff = max_backoff;
913 }
914 if (ovsdb_util_read_integer_column(row, "inactivity_probe",
915 &probe_interval)) {
916 options->probe_interval = probe_interval;
917 }
918 if (ovsdb_util_read_bool_column(row, "read_only", &read_only)) {
919 options->read_only = read_only;
920 }
921
922 free(options->role);
923 options->role = NULL;
924 if (ovsdb_util_read_string_column(row, "role", &role) && role) {
925 options->role = xstrdup(role);
926 }
927
928 options->dscp = DSCP_DEFAULT;
929 dscp_string = ovsdb_util_read_map_string_column(row, "other_config",
930 "dscp");
931 if (dscp_string) {
932 int dscp = atoi(dscp_string);
933 if (dscp >= 0 && dscp <= 63) {
934 options->dscp = dscp;
935 }
936 }
937 }
938
939 static void
940 query_db_remotes(const char *name, const struct shash *all_dbs,
941 struct shash *remotes, struct ds *errors)
942 {
943 const struct ovsdb_column *column;
944 const struct ovsdb_table *table;
945 const struct ovsdb_row *row;
946 const struct db *db;
947 char *retval;
948
949 retval = parse_db_column(all_dbs, name, &db, &table, &column);
950 if (retval) {
951 if (db && !db->db->schema) {
952 /* 'db' is a clustered database but it hasn't connected to the
953 * cluster yet, so we can't get anything out of it, not even a
954 * schema. Not really an error. */
955 } else {
956 ds_put_format(errors, "%s\n", retval);
957 }
958 free(retval);
959 return;
960 }
961
962 if (column->type.key.type == OVSDB_TYPE_STRING
963 && column->type.value.type == OVSDB_TYPE_VOID) {
964 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
965 const struct ovsdb_datum *datum;
966 size_t i;
967
968 datum = &row->fields[column->index];
969 for (i = 0; i < datum->n; i++) {
970 add_remote(remotes, datum->keys[i].string);
971 }
972 }
973 } else if (column->type.key.type == OVSDB_TYPE_UUID
974 && column->type.key.uuid.refTable
975 && column->type.value.type == OVSDB_TYPE_VOID) {
976 const struct ovsdb_table *ref_table = column->type.key.uuid.refTable;
977 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
978 const struct ovsdb_datum *datum;
979 size_t i;
980
981 datum = &row->fields[column->index];
982 for (i = 0; i < datum->n; i++) {
983 const struct ovsdb_row *ref_row;
984
985 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
986 if (ref_row) {
987 add_manager_options(remotes, ref_row);
988 }
989 }
990 }
991 }
992 }
993
994 static void
995 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
996 const struct ovsdb_jsonrpc_server *jsonrpc)
997 {
998 struct ovsdb_jsonrpc_remote_status status;
999 struct ovsdb_row *rw_row;
1000 const char *target;
1001 char *keys[9], *values[9];
1002 size_t n = 0;
1003
1004 /* Get the "target" (protocol/host/port) spec. */
1005 if (!ovsdb_util_read_string_column(row, "target", &target)) {
1006 /* Bad remote spec or incorrect schema. */
1007 return;
1008 }
1009 rw_row = ovsdb_txn_row_modify(txn, row);
1010 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
1011
1012 /* Update status information columns. */
1013 ovsdb_util_write_bool_column(rw_row, "is_connected", status.is_connected);
1014
1015 if (status.state) {
1016 keys[n] = xstrdup("state");
1017 values[n++] = xstrdup(status.state);
1018 }
1019 if (status.sec_since_connect != UINT_MAX) {
1020 keys[n] = xstrdup("sec_since_connect");
1021 values[n++] = xasprintf("%u", status.sec_since_connect);
1022 }
1023 if (status.sec_since_disconnect != UINT_MAX) {
1024 keys[n] = xstrdup("sec_since_disconnect");
1025 values[n++] = xasprintf("%u", status.sec_since_disconnect);
1026 }
1027 if (status.last_error) {
1028 keys[n] = xstrdup("last_error");
1029 values[n++] =
1030 xstrdup(ovs_retval_to_string(status.last_error));
1031 }
1032 if (status.locks_held && status.locks_held[0]) {
1033 keys[n] = xstrdup("locks_held");
1034 values[n++] = xstrdup(status.locks_held);
1035 }
1036 if (status.locks_waiting && status.locks_waiting[0]) {
1037 keys[n] = xstrdup("locks_waiting");
1038 values[n++] = xstrdup(status.locks_waiting);
1039 }
1040 if (status.locks_lost && status.locks_lost[0]) {
1041 keys[n] = xstrdup("locks_lost");
1042 values[n++] = xstrdup(status.locks_lost);
1043 }
1044 if (status.n_connections > 1) {
1045 keys[n] = xstrdup("n_connections");
1046 values[n++] = xasprintf("%d", status.n_connections);
1047 }
1048 if (status.bound_port != htons(0)) {
1049 keys[n] = xstrdup("bound_port");
1050 values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
1051 }
1052 ovsdb_util_write_string_string_column(rw_row, "status", keys, values, n);
1053
1054 ovsdb_jsonrpc_server_free_remote_status(&status);
1055 }
1056
1057 static void
1058 update_remote_rows(const struct shash *all_dbs, const struct db *db_,
1059 const char *remote_name,
1060 const struct ovsdb_jsonrpc_server *jsonrpc,
1061 struct ovsdb_txn *txn)
1062 {
1063 const struct ovsdb_table *table, *ref_table;
1064 const struct ovsdb_column *column;
1065 const struct ovsdb_row *row;
1066 const struct db *db;
1067 char *retval;
1068
1069 if (strncmp("db:", remote_name, 3)) {
1070 return;
1071 }
1072
1073 retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
1074 if (retval) {
1075 free(retval);
1076 return;
1077 }
1078
1079 if (db != db_
1080 || column->type.key.type != OVSDB_TYPE_UUID
1081 || !column->type.key.uuid.refTable
1082 || column->type.value.type != OVSDB_TYPE_VOID) {
1083 return;
1084 }
1085
1086 ref_table = column->type.key.uuid.refTable;
1087
1088 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
1089 const struct ovsdb_datum *datum;
1090 size_t i;
1091
1092 datum = &row->fields[column->index];
1093 for (i = 0; i < datum->n; i++) {
1094 const struct ovsdb_row *ref_row;
1095
1096 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
1097 if (ref_row) {
1098 update_remote_row(ref_row, txn, jsonrpc);
1099 }
1100 }
1101 }
1102 }
1103
1104 static void
1105 commit_txn(struct ovsdb_txn *txn, const char *name)
1106 {
1107 struct ovsdb_error *error = ovsdb_txn_propose_commit_block(txn, false);
1108 if (error) {
1109 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1110 char *msg = ovsdb_error_to_string_free(error);
1111 VLOG_ERR_RL(&rl, "Failed to update %s: %s", name, msg);
1112 free(msg);
1113 }
1114 }
1115
1116 static void
1117 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
1118 const struct sset *remotes,
1119 struct shash *all_dbs)
1120 {
1121 struct shash_node *node;
1122 SHASH_FOR_EACH (node, all_dbs) {
1123 struct db *db = node->data;
1124 if (!db->db || ovsdb_storage_is_clustered(db->db->storage)) {
1125 continue;
1126 }
1127
1128 struct ovsdb_txn *txn = ovsdb_txn_create(db->db);
1129 const char *remote;
1130 SSET_FOR_EACH (remote, remotes) {
1131 update_remote_rows(all_dbs, db, remote, jsonrpc, txn);
1132 }
1133 commit_txn(txn, "remote status");
1134 }
1135 }
1136
1137 /* Updates 'row', a row in the _Server database's Database table, to match
1138 * 'db'. */
1139 static void
1140 update_database_status(struct ovsdb_row *row, struct db *db)
1141 {
1142 ovsdb_util_write_string_column(row, "name", db->db->name);
1143 ovsdb_util_write_string_column(row, "model",
1144 ovsdb_storage_get_model(db->db->storage));
1145 ovsdb_util_write_bool_column(row, "connected",
1146 ovsdb_storage_is_connected(db->db->storage));
1147 ovsdb_util_write_bool_column(row, "leader",
1148 ovsdb_storage_is_leader(db->db->storage));
1149 ovsdb_util_write_uuid_column(row, "cid",
1150 ovsdb_storage_get_cid(db->db->storage));
1151 ovsdb_util_write_uuid_column(row, "sid",
1152 ovsdb_storage_get_sid(db->db->storage));
1153
1154 uint64_t index = ovsdb_storage_get_applied_index(db->db->storage);
1155 if (index) {
1156 ovsdb_util_write_integer_column(row, "index", index);
1157 } else {
1158 ovsdb_util_clear_column(row, "index");
1159 }
1160
1161 const struct uuid *row_uuid = ovsdb_row_get_uuid(row);
1162 if (!uuid_equals(row_uuid, &db->row_uuid)) {
1163 db->row_uuid = *row_uuid;
1164
1165 /* The schema can only change if the row UUID changes, so only update
1166 * it in that case. Presumably, this is worth optimizing because
1167 * schemas are often kilobytes in size and nontrivial to serialize. */
1168 char *schema = NULL;
1169 if (db->db->schema) {
1170 struct json *json_schema = ovsdb_schema_to_json(db->db->schema);
1171 schema = json_to_string(json_schema, JSSF_SORT);
1172 json_destroy(json_schema);
1173 }
1174 ovsdb_util_write_string_column(row, "schema", schema);
1175 free(schema);
1176 }
1177 }
1178
1179 /* Updates the Database table in the _Server database. */
1180 static void
1181 update_server_status(struct shash *all_dbs)
1182 {
1183 struct db *server_db = shash_find_data(all_dbs, "_Server");
1184 struct ovsdb_table *database_table = shash_find_data(
1185 &server_db->db->tables, "Database");
1186 struct ovsdb_txn *txn = ovsdb_txn_create(server_db->db);
1187
1188 /* Update rows for databases that still exist.
1189 * Delete rows for databases that no longer exist. */
1190 const struct ovsdb_row *row, *next_row;
1191 HMAP_FOR_EACH_SAFE (row, next_row, hmap_node, &database_table->rows) {
1192 const char *name;
1193 ovsdb_util_read_string_column(row, "name", &name);
1194 struct db *db = shash_find_data(all_dbs, name);
1195 if (!db || !db->db) {
1196 ovsdb_txn_row_delete(txn, row);
1197 } else {
1198 update_database_status(ovsdb_txn_row_modify(txn, row), db);
1199 }
1200 }
1201
1202 /* Add rows for new databases.
1203 *
1204 * This is O(n**2) but usually there are only 2 or 3 databases. */
1205 struct shash_node *node;
1206 SHASH_FOR_EACH (node, all_dbs) {
1207 struct db *db = node->data;
1208
1209 if (!db->db) {
1210 continue;
1211 }
1212
1213 HMAP_FOR_EACH (row, hmap_node, &database_table->rows) {
1214 const char *name;
1215 ovsdb_util_read_string_column(row, "name", &name);
1216 if (!strcmp(name, node->name)) {
1217 goto next;
1218 }
1219 }
1220
1221 /* Add row. */
1222 struct ovsdb_row *new_row = ovsdb_row_create(database_table);
1223 uuid_generate(ovsdb_row_get_uuid_rw(new_row));
1224 update_database_status(new_row, db);
1225 ovsdb_txn_row_insert(txn, new_row);
1226
1227 next:;
1228 }
1229
1230 commit_txn(txn, "_Server");
1231 }
1232
1233 /* Reconfigures ovsdb-server's remotes based on information in the database. */
1234 static char *
1235 reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
1236 const struct shash *all_dbs, struct sset *remotes)
1237 {
1238 struct ds errors = DS_EMPTY_INITIALIZER;
1239 struct shash resolved_remotes;
1240 const char *name;
1241
1242 /* Configure remotes. */
1243 shash_init(&resolved_remotes);
1244 SSET_FOR_EACH (name, remotes) {
1245 if (!strncmp(name, "db:", 3)) {
1246 query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
1247 } else {
1248 add_remote(&resolved_remotes, name);
1249 }
1250 }
1251 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
1252 free_remotes(&resolved_remotes);
1253
1254 return errors.string;
1255 }
1256
1257 static char *
1258 reconfigure_ssl(const struct shash *all_dbs)
1259 {
1260 struct ds errors = DS_EMPTY_INITIALIZER;
1261 const char *resolved_private_key;
1262 const char *resolved_certificate;
1263 const char *resolved_ca_cert;
1264 const char *resolved_ssl_protocols;
1265 const char *resolved_ssl_ciphers;
1266
1267 resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
1268 resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
1269 resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
1270 resolved_ssl_protocols = query_db_string(all_dbs, ssl_protocols, &errors);
1271 resolved_ssl_ciphers = query_db_string(all_dbs, ssl_ciphers, &errors);
1272
1273 stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
1274 stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
1275 stream_ssl_set_protocols(resolved_ssl_protocols);
1276 stream_ssl_set_ciphers(resolved_ssl_ciphers);
1277
1278 return errors.string;
1279 }
1280
1281 static void
1282 report_error_if_changed(char *error, char **last_errorp)
1283 {
1284 if (error) {
1285 if (!*last_errorp || strcmp(error, *last_errorp)) {
1286 VLOG_WARN("%s", error);
1287 free(*last_errorp);
1288 *last_errorp = error;
1289 return;
1290 }
1291 free(error);
1292 } else {
1293 free(*last_errorp);
1294 *last_errorp = NULL;
1295 }
1296 }
1297
1298 static void
1299 ovsdb_server_set_active_ovsdb_server(struct unixctl_conn *conn,
1300 int argc OVS_UNUSED, const char *argv[],
1301 void *config_)
1302 {
1303 struct server_config *config = config_;
1304
1305 if (*config->sync_from) {
1306 free(*config->sync_from);
1307 }
1308 *config->sync_from = xstrdup(argv[1]);
1309 save_config(config);
1310
1311 unixctl_command_reply(conn, NULL);
1312 }
1313
1314 static void
1315 ovsdb_server_get_active_ovsdb_server(struct unixctl_conn *conn,
1316 int argc OVS_UNUSED,
1317 const char *argv[] OVS_UNUSED,
1318 void *config_ )
1319 {
1320 struct server_config *config = config_;
1321
1322 unixctl_command_reply(conn, *config->sync_from);
1323 }
1324
1325 static void
1326 ovsdb_server_connect_active_ovsdb_server(struct unixctl_conn *conn,
1327 int argc OVS_UNUSED,
1328 const char *argv[] OVS_UNUSED,
1329 void *config_)
1330 {
1331 struct server_config *config = config_;
1332 char *msg = NULL;
1333
1334 if ( !*config->sync_from) {
1335 msg = "Unable to connect: active server is not specified.\n";
1336 } else {
1337 const struct uuid *server_uuid;
1338 server_uuid = ovsdb_jsonrpc_server_get_uuid(config->jsonrpc);
1339 ovsdb_replication_init(*config->sync_from, *config->sync_exclude,
1340 config->all_dbs, server_uuid,
1341 *config->replication_probe_interval);
1342 if (!*config->is_backup) {
1343 *config->is_backup = true;
1344 save_config(config);
1345 }
1346 }
1347 unixctl_command_reply(conn, msg);
1348 }
1349
1350 static void
1351 ovsdb_server_disconnect_active_ovsdb_server(struct unixctl_conn *conn,
1352 int argc OVS_UNUSED,
1353 const char *argv[] OVS_UNUSED,
1354 void *config_)
1355 {
1356 struct server_config *config = config_;
1357
1358 disconnect_active_server();
1359 *config->is_backup = false;
1360 save_config(config);
1361 unixctl_command_reply(conn, NULL);
1362 }
1363
1364 static void
1365 ovsdb_server_set_active_ovsdb_server_probe_interval(struct unixctl_conn *conn,
1366 int argc OVS_UNUSED,
1367 const char *argv[],
1368 void *config_)
1369 {
1370 struct server_config *config = config_;
1371
1372 int probe_interval;
1373 if (str_to_int(argv[1], 10, &probe_interval)) {
1374 *config->replication_probe_interval = probe_interval;
1375 save_config(config);
1376 if (*config->is_backup) {
1377 replication_set_probe_interval(probe_interval);
1378 }
1379 unixctl_command_reply(conn, NULL);
1380 } else {
1381 unixctl_command_reply(
1382 conn, "Invalid probe interval, integer value expected");
1383 }
1384 }
1385
1386 static void
1387 ovsdb_server_set_sync_exclude_tables(struct unixctl_conn *conn,
1388 int argc OVS_UNUSED,
1389 const char *argv[],
1390 void *config_)
1391 {
1392 struct server_config *config = config_;
1393
1394 char *err = set_excluded_tables(argv[1], true);
1395 if (!err) {
1396 free(*config->sync_exclude);
1397 *config->sync_exclude = xstrdup(argv[1]);
1398 save_config(config);
1399 if (*config->is_backup) {
1400 const struct uuid *server_uuid;
1401 server_uuid = ovsdb_jsonrpc_server_get_uuid(config->jsonrpc);
1402 ovsdb_replication_init(*config->sync_from, *config->sync_exclude,
1403 config->all_dbs, server_uuid,
1404 *config->replication_probe_interval);
1405 }
1406 err = set_excluded_tables(argv[1], false);
1407 }
1408 unixctl_command_reply(conn, err);
1409 free(err);
1410 }
1411
1412 static void
1413 ovsdb_server_get_sync_exclude_tables(struct unixctl_conn *conn,
1414 int argc OVS_UNUSED,
1415 const char *argv[] OVS_UNUSED,
1416 void *arg_ OVS_UNUSED)
1417 {
1418 char *reply = get_excluded_tables();
1419 unixctl_command_reply(conn, reply);
1420 free(reply);
1421 }
1422
1423 static void
1424 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
1425 const char *argv[] OVS_UNUSED,
1426 void *exiting_)
1427 {
1428 bool *exiting = exiting_;
1429 *exiting = true;
1430 unixctl_command_reply(conn, NULL);
1431 }
1432
1433 static void
1434 ovsdb_server_perf_counters_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1435 const char *argv[] OVS_UNUSED,
1436 void *arg_ OVS_UNUSED)
1437 {
1438 char *s = perf_counters_to_string();
1439
1440 unixctl_command_reply(conn, s);
1441 free(s);
1442 }
1443
1444 static void
1445 ovsdb_server_perf_counters_clear(struct unixctl_conn *conn, int argc OVS_UNUSED,
1446 const char *argv[] OVS_UNUSED,
1447 void *arg_ OVS_UNUSED)
1448 {
1449 perf_counters_clear();
1450 unixctl_command_reply(conn, NULL);
1451 }
1452
1453 /* "ovsdb-server/disable-monitor-cond": makes ovsdb-server drop all of its
1454 * JSON-RPC connections and reconnect. New sessions will not recognize
1455 * the 'monitor_cond' method. */
1456 static void
1457 ovsdb_server_disable_monitor_cond(struct unixctl_conn *conn,
1458 int argc OVS_UNUSED,
1459 const char *argv[] OVS_UNUSED,
1460 void *jsonrpc_)
1461 {
1462 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1463
1464 ovsdb_jsonrpc_disable_monitor_cond();
1465 ovsdb_jsonrpc_server_reconnect(
1466 jsonrpc, true, xstrdup("user ran ovsdb-server/disable-monitor-cond"));
1467 unixctl_command_reply(conn, NULL);
1468 }
1469
1470 static void
1471 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
1472 const char *argv[], void *dbs_)
1473 {
1474 const char *db_name = argc < 2 ? NULL : argv[1];
1475 struct shash *all_dbs = dbs_;
1476 struct ds reply;
1477 struct shash_node *node;
1478 int n = 0;
1479
1480 if (db_name && db_name[0] == '_') {
1481 unixctl_command_reply_error(conn, "cannot compact built-in databases");
1482 return;
1483 }
1484
1485 ds_init(&reply);
1486 SHASH_FOR_EACH(node, all_dbs) {
1487 struct db *db = node->data;
1488 if (db_name
1489 ? !strcmp(node->name, db_name)
1490 : node->name[0] != '_') {
1491 if (db->db) {
1492 VLOG_INFO("compacting %s database by user request",
1493 node->name);
1494
1495 struct ovsdb_error *error = ovsdb_snapshot(db->db);
1496 if (error) {
1497 char *s = ovsdb_error_to_string(error);
1498 ds_put_format(&reply, "%s\n", s);
1499 free(s);
1500 ovsdb_error_destroy(error);
1501 }
1502
1503 n++;
1504 }
1505 }
1506 }
1507
1508 if (!n) {
1509 unixctl_command_reply_error(conn, "no database by that name");
1510 } else if (reply.length) {
1511 unixctl_command_reply_error(conn, ds_cstr(&reply));
1512 } else {
1513 unixctl_command_reply(conn, NULL);
1514 }
1515 ds_destroy(&reply);
1516 }
1517
1518 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1519 * connections and reconnect. */
1520 static void
1521 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
1522 const char *argv[] OVS_UNUSED, void *jsonrpc_)
1523 {
1524 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1525 ovsdb_jsonrpc_server_reconnect(
1526 jsonrpc, true, xstrdup("user ran ovsdb-server/reconnect"));
1527 unixctl_command_reply(conn, NULL);
1528 }
1529
1530 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1531 * ovsdb-server services. */
1532 static void
1533 ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1534 const char *argv[], void *config_)
1535 {
1536 struct server_config *config = config_;
1537 const char *remote = argv[1];
1538
1539 const struct ovsdb_column *column;
1540 const struct ovsdb_table *table;
1541 const struct db *db;
1542 char *retval;
1543
1544 retval = (strncmp("db:", remote, 3)
1545 ? NULL
1546 : parse_db_column(config->all_dbs, remote,
1547 &db, &table, &column));
1548 if (!retval) {
1549 if (sset_add(config->remotes, remote)) {
1550 save_config(config);
1551 }
1552 unixctl_command_reply(conn, NULL);
1553 } else {
1554 unixctl_command_reply_error(conn, retval);
1555 free(retval);
1556 }
1557 }
1558
1559 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1560 * that ovsdb-server services. */
1561 static void
1562 ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1563 const char *argv[], void *config_)
1564 {
1565 struct server_config *config = config_;
1566 struct sset_node *node;
1567
1568 node = sset_find(config->remotes, argv[1]);
1569 if (node) {
1570 sset_delete(config->remotes, node);
1571 save_config(config);
1572 unixctl_command_reply(conn, NULL);
1573 } else {
1574 unixctl_command_reply_error(conn, "no such remote");
1575 }
1576 }
1577
1578 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1579 static void
1580 ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
1581 const char *argv[] OVS_UNUSED, void *remotes_)
1582 {
1583 struct sset *remotes = remotes_;
1584 const char **list, **p;
1585 struct ds s;
1586
1587 ds_init(&s);
1588
1589 list = sset_sort(remotes);
1590 for (p = list; *p; p++) {
1591 ds_put_format(&s, "%s\n", *p);
1592 }
1593 free(list);
1594
1595 unixctl_command_reply(conn, ds_cstr(&s));
1596 ds_destroy(&s);
1597 }
1598
1599
1600 /* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1601 static void
1602 ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1603 const char *argv[], void *config_)
1604 {
1605 struct server_config *config = config_;
1606 const char *filename = argv[1];
1607
1608 char *error = ovsdb_error_to_string_free(open_db(config, filename));
1609 if (!error) {
1610 save_config(config);
1611 if (*config->is_backup) {
1612 const struct uuid *server_uuid;
1613 server_uuid = ovsdb_jsonrpc_server_get_uuid(config->jsonrpc);
1614 ovsdb_replication_init(*config->sync_from, *config->sync_exclude,
1615 config->all_dbs, server_uuid,
1616 *config->replication_probe_interval);
1617 }
1618 unixctl_command_reply(conn, NULL);
1619 } else {
1620 unixctl_command_reply_error(conn, error);
1621 free(error);
1622 }
1623 }
1624
1625 static void
1626 remove_db(struct server_config *config, struct shash_node *node, char *comment)
1627 {
1628 struct db *db = node->data;
1629
1630 close_db(config, db, comment);
1631 shash_delete(config->all_dbs, node);
1632
1633 save_config(config);
1634 if (*config->is_backup) {
1635 const struct uuid *server_uuid;
1636 server_uuid = ovsdb_jsonrpc_server_get_uuid(config->jsonrpc);
1637 ovsdb_replication_init(*config->sync_from, *config->sync_exclude,
1638 config->all_dbs, server_uuid,
1639 *config->replication_probe_interval);
1640 }
1641 }
1642
1643 static void
1644 ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1645 const char *argv[], void *config_)
1646 {
1647 struct server_config *config = config_;
1648 struct shash_node *node;
1649
1650 node = shash_find(config->all_dbs, argv[1]);
1651 if (!node) {
1652 unixctl_command_reply_error(conn, "Failed to find the database.");
1653 return;
1654 }
1655 if (node->name[0] == '_') {
1656 unixctl_command_reply_error(conn, "Cannot remove reserved database.");
1657 return;
1658 }
1659
1660 remove_db(config, node, xasprintf("removing %s database by user request",
1661 node->name));
1662 unixctl_command_reply(conn, NULL);
1663 }
1664
1665 static void
1666 ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
1667 const char *argv[] OVS_UNUSED, void *all_dbs_)
1668 {
1669 struct shash *all_dbs = all_dbs_;
1670 const struct shash_node **nodes;
1671 struct ds s;
1672 size_t i;
1673
1674 ds_init(&s);
1675
1676 nodes = shash_sort(all_dbs);
1677 for (i = 0; i < shash_count(all_dbs); i++) {
1678 const struct shash_node *node = nodes[i];
1679 struct db *db = node->data;
1680 if (db->db) {
1681 ds_put_format(&s, "%s\n", node->name);
1682 }
1683 }
1684 free(nodes);
1685
1686 unixctl_command_reply(conn, ds_cstr(&s));
1687 ds_destroy(&s);
1688 }
1689
1690 static void
1691 ovsdb_server_get_sync_status(struct unixctl_conn *conn, int argc OVS_UNUSED,
1692 const char *argv[] OVS_UNUSED, void *config_)
1693 {
1694 struct server_config *config = config_;
1695 bool is_backup = *config->is_backup;
1696 struct ds ds = DS_EMPTY_INITIALIZER;
1697
1698 ds_put_format(&ds, "state: %s\n", is_backup ? "backup" : "active");
1699
1700 if (is_backup) {
1701 ds_put_and_free_cstr(&ds, replication_status());
1702 }
1703
1704 unixctl_command_reply(conn, ds_cstr(&ds));
1705 ds_destroy(&ds);
1706 }
1707
1708 static void
1709 ovsdb_server_get_db_storage_status(struct unixctl_conn *conn,
1710 int argc OVS_UNUSED,
1711 const char *argv[],
1712 void *config_)
1713 {
1714 struct server_config *config = config_;
1715 struct shash_node *node;
1716
1717 node = shash_find(config->all_dbs, argv[1]);
1718 if (!node) {
1719 unixctl_command_reply_error(conn, "Failed to find the database.");
1720 return;
1721 }
1722
1723 struct db *db = node->data;
1724
1725 if (!db->db) {
1726 unixctl_command_reply_error(conn, "Failed to find the database.");
1727 return;
1728 }
1729
1730 struct ds ds = DS_EMPTY_INITIALIZER;
1731 char *error = ovsdb_storage_get_error(db->db->storage);
1732
1733 if (!error) {
1734 ds_put_cstr(&ds, "status: ok");
1735 } else {
1736 ds_put_format(&ds, "status: %s", error);
1737 free(error);
1738 }
1739 unixctl_command_reply(conn, ds_cstr(&ds));
1740 ds_destroy(&ds);
1741 }
1742
1743 static void
1744 parse_options(int argc, char *argv[],
1745 struct sset *db_filenames, struct sset *remotes,
1746 char **unixctl_pathp, char **run_command,
1747 char **sync_from, char **sync_exclude, bool *active)
1748 {
1749 enum {
1750 OPT_REMOTE = UCHAR_MAX + 1,
1751 OPT_UNIXCTL,
1752 OPT_RUN,
1753 OPT_BOOTSTRAP_CA_CERT,
1754 OPT_PEER_CA_CERT,
1755 OPT_SYNC_FROM,
1756 OPT_SYNC_EXCLUDE,
1757 OPT_ACTIVE,
1758 OPT_NO_DBS,
1759 VLOG_OPTION_ENUMS,
1760 DAEMON_OPTION_ENUMS,
1761 SSL_OPTION_ENUMS,
1762 };
1763
1764 static const struct option long_options[] = {
1765 {"remote", required_argument, NULL, OPT_REMOTE},
1766 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
1767 #ifndef _WIN32
1768 {"run", required_argument, NULL, OPT_RUN},
1769 #endif
1770 {"help", no_argument, NULL, 'h'},
1771 {"version", no_argument, NULL, 'V'},
1772 DAEMON_LONG_OPTIONS,
1773 VLOG_LONG_OPTIONS,
1774 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
1775 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
1776 STREAM_SSL_LONG_OPTIONS,
1777 {"sync-from", required_argument, NULL, OPT_SYNC_FROM},
1778 {"sync-exclude-tables", required_argument, NULL, OPT_SYNC_EXCLUDE},
1779 {"active", no_argument, NULL, OPT_ACTIVE},
1780 {"no-dbs", no_argument, NULL, OPT_NO_DBS},
1781 {NULL, 0, NULL, 0},
1782 };
1783 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
1784 bool add_default_db = true;
1785
1786 *sync_from = NULL;
1787 *sync_exclude = NULL;
1788 sset_init(db_filenames);
1789 sset_init(remotes);
1790 for (;;) {
1791 int c;
1792
1793 c = getopt_long(argc, argv, short_options, long_options, NULL);
1794 if (c == -1) {
1795 break;
1796 }
1797
1798 switch (c) {
1799 case OPT_REMOTE:
1800 sset_add(remotes, optarg);
1801 break;
1802
1803 case OPT_UNIXCTL:
1804 *unixctl_pathp = optarg;
1805 break;
1806
1807 case OPT_RUN:
1808 *run_command = optarg;
1809 break;
1810
1811 case 'h':
1812 usage();
1813
1814 case 'V':
1815 ovs_print_version(0, 0);
1816 exit(EXIT_SUCCESS);
1817
1818 VLOG_OPTION_HANDLERS
1819 DAEMON_OPTION_HANDLERS
1820
1821 case 'p':
1822 private_key_file = optarg;
1823 break;
1824
1825 case 'c':
1826 certificate_file = optarg;
1827 break;
1828
1829 case 'C':
1830 ca_cert_file = optarg;
1831 bootstrap_ca_cert = false;
1832 break;
1833
1834 case OPT_SSL_PROTOCOLS:
1835 ssl_protocols = optarg;
1836 break;
1837
1838 case OPT_SSL_CIPHERS:
1839 ssl_ciphers = optarg;
1840 break;
1841
1842 case OPT_BOOTSTRAP_CA_CERT:
1843 ca_cert_file = optarg;
1844 bootstrap_ca_cert = true;
1845 break;
1846
1847 case OPT_PEER_CA_CERT:
1848 stream_ssl_set_peer_ca_cert_file(optarg);
1849 break;
1850
1851 case OPT_SYNC_FROM:
1852 *sync_from = xstrdup(optarg);
1853 break;
1854
1855 case OPT_SYNC_EXCLUDE: {
1856 char *err = set_excluded_tables(optarg, false);
1857 if (err) {
1858 ovs_fatal(0, "%s", err);
1859 }
1860 *sync_exclude = xstrdup(optarg);
1861 break;
1862 }
1863 case OPT_ACTIVE:
1864 *active = true;
1865 break;
1866
1867 case OPT_NO_DBS:
1868 add_default_db = false;
1869 break;
1870
1871 case '?':
1872 exit(EXIT_FAILURE);
1873
1874 default:
1875 abort();
1876 }
1877 }
1878 free(short_options);
1879
1880 argc -= optind;
1881 argv += optind;
1882 if (argc > 0) {
1883 for (int i = 0; i < argc; i++) {
1884 sset_add(db_filenames, argv[i]);
1885 }
1886 } else if (add_default_db) {
1887 sset_add_and_free(db_filenames, xasprintf("%s/conf.db", ovs_dbdir()));
1888 }
1889 }
1890
1891 static void
1892 usage(void)
1893 {
1894 printf("%s: Open vSwitch database server\n"
1895 "usage: %s [OPTIONS] [DATABASE...]\n"
1896 "where each DATABASE is a database file in ovsdb format.\n"
1897 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1898 program_name, program_name, ovs_dbdir());
1899 printf("\nJSON-RPC options (may be specified any number of times):\n"
1900 " --remote=REMOTE connect or listen to REMOTE\n");
1901 stream_usage("JSON-RPC", true, true, true);
1902 daemon_usage();
1903 vlog_usage();
1904 replication_usage();
1905 printf("\nOther options:\n"
1906 " --run COMMAND run COMMAND as subprocess then exit\n"
1907 " --unixctl=SOCKET override default control socket name\n"
1908 " -h, --help display this help message\n"
1909 " -V, --version display version information\n");
1910 exit(EXIT_SUCCESS);
1911 }
1912 \f
1913 static struct json *
1914 sset_to_json(const struct sset *sset)
1915 {
1916 struct json *array;
1917 const char *s;
1918
1919 array = json_array_create_empty();
1920 SSET_FOR_EACH (s, sset) {
1921 json_array_add(array, json_string_create(s));
1922 }
1923 return array;
1924 }
1925
1926 /* Truncates and replaces the contents of 'config_file' by a representation of
1927 * 'remotes' and 'db_filenames'. */
1928 static void
1929 save_config__(FILE *config_file, const struct sset *remotes,
1930 const struct sset *db_filenames, const char *sync_from,
1931 const char *sync_exclude, bool is_backup)
1932 {
1933 struct json *obj;
1934 char *s;
1935
1936 if (ftruncate(fileno(config_file), 0) == -1) {
1937 VLOG_FATAL("failed to truncate temporary file (%s)",
1938 ovs_strerror(errno));
1939 }
1940
1941 obj = json_object_create();
1942 json_object_put(obj, "remotes", sset_to_json(remotes));
1943 json_object_put(obj, "db_filenames", sset_to_json(db_filenames));
1944 if (sync_from) {
1945 json_object_put(obj, "sync_from", json_string_create(sync_from));
1946 }
1947 if (sync_exclude) {
1948 json_object_put(obj, "sync_exclude",
1949 json_string_create(sync_exclude));
1950 }
1951 json_object_put(obj, "is_backup", json_boolean_create(is_backup));
1952
1953 s = json_to_string(obj, 0);
1954 json_destroy(obj);
1955
1956 if (fseek(config_file, 0, SEEK_SET) != 0
1957 || fputs(s, config_file) == EOF
1958 || fflush(config_file) == EOF) {
1959 VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno));
1960 }
1961 free(s);
1962 }
1963
1964 /* Truncates and replaces the contents of 'config_file' by a representation of
1965 * 'config'. */
1966 static void
1967 save_config(struct server_config *config)
1968 {
1969 struct sset db_filenames;
1970 struct shash_node *node;
1971
1972 sset_init(&db_filenames);
1973 SHASH_FOR_EACH (node, config->all_dbs) {
1974 struct db *db = node->data;
1975 if (node->name[0] != '_') {
1976 sset_add(&db_filenames, db->filename);
1977 }
1978 }
1979
1980 save_config__(config->config_tmpfile, config->remotes, &db_filenames,
1981 *config->sync_from, *config->sync_exclude,
1982 *config->is_backup);
1983
1984 sset_destroy(&db_filenames);
1985 }
1986
1987 static void
1988 sset_from_json(struct sset *sset, const struct json *array)
1989 {
1990 size_t i;
1991
1992 sset_clear(sset);
1993
1994 ovs_assert(array->type == JSON_ARRAY);
1995 for (i = 0; i < array->array.n; i++) {
1996 const struct json *elem = array->array.elems[i];
1997 sset_add(sset, json_string(elem));
1998 }
1999 }
2000
2001 /* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
2002 * 'config_file', which must have been previously written by save_config(). */
2003 static void
2004 load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames,
2005 char **sync_from, char **sync_exclude, bool *is_backup)
2006 {
2007 struct json *json;
2008
2009 if (fseek(config_file, 0, SEEK_SET) != 0) {
2010 VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno));
2011 }
2012 json = json_from_stream(config_file);
2013 if (json->type == JSON_STRING) {
2014 VLOG_FATAL("reading json failed (%s)", json_string(json));
2015 }
2016 ovs_assert(json->type == JSON_OBJECT);
2017
2018 sset_from_json(remotes, shash_find_data(json_object(json), "remotes"));
2019 sset_from_json(db_filenames,
2020 shash_find_data(json_object(json), "db_filenames"));
2021
2022 struct json *string;
2023 string = shash_find_data(json_object(json), "sync_from");
2024 free(*sync_from);
2025 *sync_from = string ? xstrdup(json_string(string)) : NULL;
2026
2027 string = shash_find_data(json_object(json), "sync_exclude");
2028 free(*sync_exclude);
2029 *sync_exclude = string ? xstrdup(json_string(string)) : NULL;
2030
2031 *is_backup = json_boolean(shash_find_data(json_object(json), "is_backup"));
2032
2033 json_destroy(json);
2034 }