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