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