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