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