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