]> git.proxmox.com Git - ovs.git/blame - ovsdb/ovsdb-server.c
test-sha1: Remove unneeded casts.
[ovs.git] / ovsdb / ovsdb-server.c
CommitLineData
0d085684 1/* Copyright (c) 2009, 2010, 2011, 2012 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
da897f41 18#include <assert.h>
f85f8ebb
BP
19#include <errno.h>
20#include <getopt.h>
21#include <signal.h>
eb077b26 22#include <unistd.h>
f85f8ebb 23
0b1fae1b 24#include "column.h"
f85f8ebb
BP
25#include "command-line.h"
26#include "daemon.h"
29701194 27#include "dirs.h"
bd06962a 28#include "file.h"
da897f41 29#include "hash.h"
f85f8ebb
BP
30#include "json.h"
31#include "jsonrpc.h"
32#include "jsonrpc-server.h"
33#include "leak-checker.h"
34#include "list.h"
0d085684 35#include "memory.h"
da897f41 36#include "ovsdb.h"
0b1fae1b
BP
37#include "ovsdb-data.h"
38#include "ovsdb-types.h"
f85f8ebb
BP
39#include "ovsdb-error.h"
40#include "poll-loop.h"
41#include "process.h"
0b1fae1b 42#include "row.h"
0d085684 43#include "simap.h"
9467fe62 44#include "stream-ssl.h"
f85f8ebb 45#include "stream.h"
cc01d0bb 46#include "stress.h"
b3c01ed3 47#include "sset.h"
0b1fae1b 48#include "table.h"
f85f8ebb 49#include "timeval.h"
0b3e7a8b 50#include "transaction.h"
f85f8ebb
BP
51#include "trigger.h"
52#include "util.h"
53#include "unixctl.h"
f85f8ebb 54#include "vlog.h"
5136ce49 55
d98e6007 56VLOG_DEFINE_THIS_MODULE(ovsdb_server);
f85f8ebb 57
78876719
BP
58/* SSL configuration. */
59static char *private_key_file;
60static char *certificate_file;
61static char *ca_cert_file;
62static bool bootstrap_ca_cert;
63
aa78de9d 64static unixctl_cb_func ovsdb_server_exit;
ada496b5 65static unixctl_cb_func ovsdb_server_compact;
31d0b6c9 66static unixctl_cb_func ovsdb_server_reconnect;
aa78de9d 67
f85f8ebb 68static void parse_options(int argc, char *argv[], char **file_namep,
b3c01ed3 69 struct sset *remotes, char **unixctl_pathp,
475afa1b 70 char **run_command);
f85f8ebb
BP
71static void usage(void) NO_RETURN;
72
78876719 73static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
b3c01ed3 74 const struct ovsdb *db, struct sset *remotes);
0b1fae1b 75
0b3e7a8b 76static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
b3c01ed3 77 const struct sset *remotes,
0b3e7a8b
AE
78 struct ovsdb *db);
79
f85f8ebb
BP
80int
81main(int argc, char *argv[])
82{
aa78de9d 83 char *unixctl_path = NULL;
475afa1b 84 char *run_command = NULL;
f85f8ebb
BP
85 struct unixctl_server *unixctl;
86 struct ovsdb_jsonrpc_server *jsonrpc;
b3c01ed3 87 struct sset remotes;
f85f8ebb 88 struct ovsdb_error *error;
ada496b5 89 struct ovsdb_file *file;
f85f8ebb 90 struct ovsdb *db;
475afa1b 91 struct process *run_process;
f85f8ebb 92 char *file_name;
aa78de9d 93 bool exiting;
f85f8ebb 94 int retval;
0b3e7a8b 95 long long int status_timer = LLONG_MIN;
f85f8ebb 96
40f0707c 97 proctitle_init(argc, argv);
f85f8ebb 98 set_program_name(argv[0]);
cc01d0bb 99 stress_init_command();
f85f8ebb
BP
100 signal(SIGPIPE, SIG_IGN);
101 process_init();
102
475afa1b
BP
103 parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
104 &run_command);
f85f8ebb 105
95440284 106 daemonize_start();
eb077b26 107
ada496b5 108 error = ovsdb_file_open(file_name, false, &db, &file);
f85f8ebb
BP
109 if (error) {
110 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
111 }
29701194 112 free(file_name);
f85f8ebb 113
b93d3b6c 114 jsonrpc = ovsdb_jsonrpc_server_create(db);
78876719 115 reconfigure_from_db(jsonrpc, db, &remotes);
f85f8ebb 116
aa78de9d 117 retval = unixctl_server_create(unixctl_path, &unixctl);
f85f8ebb 118 if (retval) {
4d12270a 119 exit(EXIT_FAILURE);
f85f8ebb
BP
120 }
121
475afa1b
BP
122 if (run_command) {
123 char *run_argv[4];
124
125 run_argv[0] = "/bin/sh";
126 run_argv[1] = "-c";
127 run_argv[2] = run_command;
128 run_argv[3] = NULL;
129
130 retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
131 if (retval) {
132 ovs_fatal(retval, "%s: process failed to start", run_command);
133 }
134 } else {
135 run_process = NULL;
136 }
137
95440284 138 daemonize_complete();
aa78de9d 139
08b9b190
EJ
140 if (!run_command) {
141 /* ovsdb-server is usually a long-running process, in which case it
142 * makes plenty of sense to log the version, but --run makes
143 * ovsdb-server more like a command-line tool, so skip it. */
144 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
145 }
9dbc190c 146
0e15264f
BP
147 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
148 unixctl_command_register("ovsdb-server/compact", "", 0, 0,
7ff2009a 149 ovsdb_server_compact, file);
0e15264f 150 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
7ff2009a 151 ovsdb_server_reconnect, jsonrpc);
aa78de9d
BP
152
153 exiting = false;
154 while (!exiting) {
0d085684
BP
155 memory_run();
156 if (memory_should_report()) {
157 struct simap usage;
158
159 simap_init(&usage);
160 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
161 ovsdb_get_memory_usage(db, &usage);
162 memory_report(&usage);
163 simap_destroy(&usage);
164 }
165
78876719 166 reconfigure_from_db(jsonrpc, db, &remotes);
f85f8ebb
BP
167 ovsdb_jsonrpc_server_run(jsonrpc);
168 unixctl_server_run(unixctl);
169 ovsdb_trigger_run(db, time_msec());
475afa1b
BP
170 if (run_process && process_exited(run_process)) {
171 exiting = true;
172 }
f85f8ebb 173
0b3e7a8b
AE
174 /* update Manager status(es) every 5 seconds */
175 if (time_msec() >= status_timer) {
176 status_timer = time_msec() + 5000;
177 update_remote_status(jsonrpc, &remotes, db);
178 }
179
0d085684 180 memory_wait();
f85f8ebb
BP
181 ovsdb_jsonrpc_server_wait(jsonrpc);
182 unixctl_server_wait(unixctl);
183 ovsdb_trigger_wait(db, time_msec());
475afa1b
BP
184 if (run_process) {
185 process_wait(run_process);
186 }
6d37aaf1
BP
187 if (exiting) {
188 poll_immediate_wake();
189 }
0b3e7a8b 190 poll_timer_wait_until(status_timer);
f85f8ebb
BP
191 poll_block();
192 }
23935e8b
BP
193 ovsdb_jsonrpc_server_destroy(jsonrpc);
194 ovsdb_destroy(db);
b3c01ed3 195 sset_destroy(&remotes);
23935e8b 196 unixctl_server_destroy(unixctl);
f85f8ebb 197
475afa1b
BP
198 if (run_process && process_exited(run_process)) {
199 int status = process_status(run_process);
200 if (status) {
201 ovs_fatal(0, "%s: child exited, %s",
202 run_command, process_status_msg(status));
203 }
204 }
205
f85f8ebb
BP
206 return 0;
207}
208
0b1fae1b 209static void
94db5407
BP
210parse_db_column(const struct ovsdb *db,
211 const char *name_,
212 const struct ovsdb_table **tablep,
213 const struct ovsdb_column **columnp)
0b1fae1b 214{
b0ef0551 215 char *name, *table_name, *column_name;
0b1fae1b
BP
216 const struct ovsdb_column *column;
217 const struct ovsdb_table *table;
0b1fae1b
BP
218 char *save_ptr = NULL;
219
220 name = xstrdup(name_);
b0ef0551 221 strtok_r(name, ":", &save_ptr); /* "db:" */
0b1fae1b
BP
222 table_name = strtok_r(NULL, ",", &save_ptr);
223 column_name = strtok_r(NULL, ",", &save_ptr);
224 if (!table_name || !column_name) {
78876719 225 ovs_fatal(0, "\"%s\": invalid syntax", name_);
0b1fae1b
BP
226 }
227
228 table = ovsdb_get_table(db, table_name);
229 if (!table) {
78876719 230 ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
0b1fae1b
BP
231 }
232
233 column = ovsdb_table_schema_get_column(table->schema, column_name);
234 if (!column) {
78876719 235 ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
0b1fae1b
BP
236 name_, table_name, column_name);
237 }
78876719 238 free(name);
0b1fae1b 239
94db5407
BP
240 *columnp = column;
241 *tablep = table;
242}
243
244static void
245parse_db_string_column(const struct ovsdb *db,
246 const char *name,
247 const struct ovsdb_table **tablep,
248 const struct ovsdb_column **columnp)
249{
250 const struct ovsdb_column *column;
251 const struct ovsdb_table *table;
252
253 parse_db_column(db, name, &table, &column);
254
bd76d25d
BP
255 if (column->type.key.type != OVSDB_TYPE_STRING
256 || column->type.value.type != OVSDB_TYPE_VOID) {
78876719 257 ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
0b1fae1b 258 "not string or set of strings",
94db5407 259 name, table->schema->name, column->name);
78876719
BP
260 }
261
262 *columnp = column;
263 *tablep = table;
264}
265
a02f8286 266static OVS_UNUSED const char *
78876719
BP
267query_db_string(const struct ovsdb *db, const char *name)
268{
269 if (!name || strncmp(name, "db:", 3)) {
270 return name;
271 } else {
272 const struct ovsdb_column *column;
273 const struct ovsdb_table *table;
274 const struct ovsdb_row *row;
275
276 parse_db_string_column(db, name, &table, &column);
277
4e8e4213 278 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
78876719
BP
279 const struct ovsdb_datum *datum;
280 size_t i;
281
282 datum = &row->fields[column->index];
283 for (i = 0; i < datum->n; i++) {
284 if (datum->keys[i].string[0]) {
285 return datum->keys[i].string;
286 }
287 }
288 }
289 return NULL;
0b1fae1b 290 }
78876719
BP
291}
292
94db5407
BP
293static struct ovsdb_jsonrpc_options *
294add_remote(struct shash *remotes, const char *target)
295{
296 struct ovsdb_jsonrpc_options *options;
297
298 options = shash_find_data(remotes, target);
299 if (!options) {
f1936eb6 300 options = ovsdb_jsonrpc_default_options(target);
94db5407
BP
301 shash_add(remotes, target, options);
302 }
303
304 return options;
305}
306
0b3e7a8b
AE
307static struct ovsdb_datum *
308get_datum(struct ovsdb_row *row, const char *column_name,
309 const enum ovsdb_atomic_type key_type,
310 const enum ovsdb_atomic_type value_type,
311 const size_t n_max)
94db5407
BP
312{
313 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
314 const struct ovsdb_table_schema *schema = row->table->schema;
315 const struct ovsdb_column *column;
94db5407
BP
316
317 column = ovsdb_table_schema_get_column(schema, column_name);
318 if (!column) {
319 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
320 schema->name, column_name);
0b3e7a8b 321 return NULL;
94db5407
BP
322 }
323
0b3e7a8b
AE
324 if (column->type.key.type != key_type
325 || column->type.value.type != value_type
326 || column->type.n_max != n_max) {
94db5407
BP
327 if (!VLOG_DROP_DBG(&rl)) {
328 char *type_name = ovsdb_type_to_english(&column->type);
329 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
0b3e7a8b
AE
330 "key type %s, value type %s, max elements %zd.",
331 schema->name, column_name, type_name,
332 ovsdb_atomic_type_to_string(key_type),
333 ovsdb_atomic_type_to_string(value_type),
334 n_max);
335 free(type_name);
94db5407 336 }
0b3e7a8b 337 return NULL;
94db5407
BP
338 }
339
0b3e7a8b
AE
340 return &row->fields[column->index];
341}
342
cea15768
EJ
343/* Read string-string key-values from a map. Returns the value associated with
344 * 'key', if found, or NULL */
345static const char *
f125905c 346read_map_string_column(const struct ovsdb_row *row, const char *column_name,
cea15768 347 const char *key)
f125905c
MM
348{
349 const struct ovsdb_datum *datum;
350 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
351 size_t i;
352
353 datum = get_datum((struct ovsdb_row *) row, column_name, OVSDB_TYPE_STRING,
354 OVSDB_TYPE_STRING, UINT_MAX);
355
356 if (!datum) {
cea15768 357 return NULL;
f125905c
MM
358 }
359
360 for (i = 0; i < datum->n; i++) {
361 atom_key = &datum->keys[i];
362 if (!strcmp(atom_key->string, key)){
363 atom_value = &datum->values[i];
364 break;
365 }
366 }
367
cea15768 368 return atom_value ? atom_value->string : NULL;
f125905c
MM
369}
370
0b3e7a8b
AE
371static const union ovsdb_atom *
372read_column(const struct ovsdb_row *row, const char *column_name,
373 enum ovsdb_atomic_type type)
374{
375 const struct ovsdb_datum *datum;
376
f125905c
MM
377 datum = get_datum((struct ovsdb_row *) row, column_name, type, OVSDB_TYPE_VOID,
378 1);
0b3e7a8b 379 return datum && datum->n ? datum->keys : NULL;
94db5407
BP
380}
381
382static bool
383read_integer_column(const struct ovsdb_row *row, const char *column_name,
384 long long int *integerp)
385{
386 const union ovsdb_atom *atom;
387
388 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
389 *integerp = atom ? atom->integer : 0;
390 return atom != NULL;
391}
392
393static bool
394read_string_column(const struct ovsdb_row *row, const char *column_name,
395 const char **stringp)
396{
397 const union ovsdb_atom *atom;
398
399 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
e3c17733 400 *stringp = atom ? atom->string : NULL;
94db5407
BP
401 return atom != NULL;
402}
403
0b3e7a8b
AE
404static void
405write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
406{
407 struct ovsdb_datum *datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
408 OVSDB_TYPE_VOID, 1);
409
410 if (!datum) {
411 return;
412 }
413 datum->keys[0].boolean = value;
414}
415
416static void
417write_string_string_column(struct ovsdb_row *row, const char *column_name,
418 char **keys, char **values, size_t n)
419{
420 const struct ovsdb_column *column;
421 struct ovsdb_datum *datum;
422 size_t i;
423
424 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
425 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
426 UINT_MAX);
427 if (!datum) {
428 return;
429 }
430
431 /* Free existing data. */
432 ovsdb_datum_destroy(datum, &column->type);
433
434 /* Allocate space for new values. */
435 datum->n = n;
436 datum->keys = xmalloc(n * sizeof *datum->keys);
437 datum->values = xmalloc(n * sizeof *datum->values);
438
439 for (i = 0; i < n; ++i) {
440 datum->keys[i].string = keys[i];
441 datum->values[i].string = values[i];
442 }
443
444 /* Sort and check constraints. */
445 ovsdb_datum_sort_assert(datum, column->type.key.type);
446}
447
94db5407
BP
448/* Adds a remote and options to 'remotes', based on the Manager table row in
449 * 'row'. */
450static void
451add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
452{
453 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
454 struct ovsdb_jsonrpc_options *options;
455 long long int max_backoff, probe_interval;
cea15768 456 const char *target, *dscp_string;
94db5407
BP
457
458 if (!read_string_column(row, "target", &target) || !target) {
459 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
460 row->table->schema->name);
461 return;
462 }
463
464 options = add_remote(remotes, target);
465 if (read_integer_column(row, "max_backoff", &max_backoff)) {
466 options->max_backoff = max_backoff;
467 }
f441c1a8 468 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
94db5407
BP
469 options->probe_interval = probe_interval;
470 }
f125905c 471
cea15768
EJ
472 options->dscp = DSCP_DEFAULT;
473 dscp_string = read_map_string_column(row, "other_config", "dscp");
474 if (dscp_string) {
475 int dscp = atoi(dscp_string);
476 if (dscp >= 0 && dscp <= 63) {
477 options->dscp = dscp;
478 }
479 }
94db5407
BP
480}
481
78876719
BP
482static void
483query_db_remotes(const char *name, const struct ovsdb *db,
484 struct shash *remotes)
485{
486 const struct ovsdb_column *column;
487 const struct ovsdb_table *table;
488 const struct ovsdb_row *row;
489
94db5407 490 parse_db_column(db, name, &table, &column);
0b1fae1b 491
94db5407
BP
492 if (column->type.key.type == OVSDB_TYPE_STRING
493 && column->type.value.type == OVSDB_TYPE_VOID) {
494 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
495 const struct ovsdb_datum *datum;
496 size_t i;
497
498 datum = &row->fields[column->index];
499 for (i = 0; i < datum->n; i++) {
500 add_remote(remotes, datum->keys[i].string);
501 }
502 }
503 } else if (column->type.key.type == OVSDB_TYPE_UUID
504 && column->type.key.u.uuid.refTable
505 && column->type.value.type == OVSDB_TYPE_VOID) {
506 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
507 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
508 const struct ovsdb_datum *datum;
509 size_t i;
510
511 datum = &row->fields[column->index];
512 for (i = 0; i < datum->n; i++) {
513 const struct ovsdb_row *ref_row;
0b1fae1b 514
94db5407
BP
515 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
516 if (ref_row) {
517 add_manager_options(remotes, ref_row);
518 }
519 }
0b1fae1b
BP
520 }
521 }
522}
523
0b3e7a8b
AE
524static void
525update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
87fcbc60 526 const struct ovsdb_jsonrpc_server *jsonrpc)
0b3e7a8b 527{
87fcbc60 528 struct ovsdb_jsonrpc_remote_status status;
0b3e7a8b
AE
529 struct ovsdb_row *rw_row;
530 const char *target;
87fcbc60 531 char *keys[8], *values[8];
0b3e7a8b
AE
532 size_t n = 0;
533
534 /* Get the "target" (protocol/host/port) spec. */
535 if (!read_string_column(row, "target", &target)) {
536 /* Bad remote spec or incorrect schema. */
537 return;
538 }
0b3e7a8b 539 rw_row = ovsdb_txn_row_modify(txn, row);
87fcbc60 540 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
0b3e7a8b
AE
541
542 /* Update status information columns. */
87fcbc60 543 write_bool_column(rw_row, "is_connected", status.is_connected);
0b3e7a8b 544
87fcbc60
BP
545 if (status.state) {
546 keys[n] = xstrdup("state");
547 values[n++] = xstrdup(status.state);
548 }
549 if (status.sec_since_connect != UINT_MAX) {
5eda645e 550 keys[n] = xstrdup("sec_since_connect");
87fcbc60 551 values[n++] = xasprintf("%u", status.sec_since_connect);
5eda645e 552 }
87fcbc60 553 if (status.sec_since_disconnect != UINT_MAX) {
5eda645e 554 keys[n] = xstrdup("sec_since_disconnect");
87fcbc60 555 values[n++] = xasprintf("%u", status.sec_since_disconnect);
5eda645e 556 }
87fcbc60 557 if (status.last_error) {
0b3e7a8b
AE
558 keys[n] = xstrdup("last_error");
559 values[n++] =
87fcbc60 560 xstrdup(ovs_retval_to_string(status.last_error));
0b3e7a8b 561 }
da897f41
BP
562 if (status.locks_held && status.locks_held[0]) {
563 keys[n] = xstrdup("locks_held");
564 values[n++] = xstrdup(status.locks_held);
565 }
566 if (status.locks_waiting && status.locks_waiting[0]) {
567 keys[n] = xstrdup("locks_waiting");
568 values[n++] = xstrdup(status.locks_waiting);
569 }
570 if (status.locks_lost && status.locks_lost[0]) {
571 keys[n] = xstrdup("locks_lost");
572 values[n++] = xstrdup(status.locks_lost);
573 }
a11f6164
BP
574 if (status.n_connections > 1) {
575 keys[n] = xstrdup("n_connections");
576 values[n++] = xasprintf("%d", status.n_connections);
577 }
0b3e7a8b 578 write_string_string_column(rw_row, "status", keys, values, n);
da897f41
BP
579
580 ovsdb_jsonrpc_server_free_remote_status(&status);
0b3e7a8b
AE
581}
582
583static void
584update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
87fcbc60
BP
585 const char *remote_name,
586 const struct ovsdb_jsonrpc_server *jsonrpc)
0b3e7a8b
AE
587{
588 const struct ovsdb_table *table, *ref_table;
589 const struct ovsdb_column *column;
590 const struct ovsdb_row *row;
591
592 if (strncmp("db:", remote_name, 3)) {
593 return;
594 }
595
596 parse_db_column(db, remote_name, &table, &column);
597
598 if (column->type.key.type != OVSDB_TYPE_UUID
599 || !column->type.key.u.uuid.refTable
600 || column->type.value.type != OVSDB_TYPE_VOID) {
601 return;
602 }
603
604 ref_table = column->type.key.u.uuid.refTable;
605
606 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
607 const struct ovsdb_datum *datum;
608 size_t i;
609
610 datum = &row->fields[column->index];
611 for (i = 0; i < datum->n; i++) {
612 const struct ovsdb_row *ref_row;
613
614 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
615 if (ref_row) {
87fcbc60 616 update_remote_row(ref_row, txn, jsonrpc);
0b3e7a8b
AE
617 }
618 }
619 }
620}
621
622static void
623update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
b3c01ed3 624 const struct sset *remotes, struct ovsdb *db)
0b3e7a8b
AE
625{
626 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
0b3e7a8b
AE
627 struct ovsdb_txn *txn;
628 const bool durable_txn = false;
629 struct ovsdb_error *error;
b3c01ed3 630 const char *remote;
0b3e7a8b 631
0b3e7a8b
AE
632 txn = ovsdb_txn_create(db);
633
634 /* Iterate over --remote arguments given on command line. */
b3c01ed3 635 SSET_FOR_EACH (remote, remotes) {
87fcbc60 636 update_remote_rows(db, txn, remote, jsonrpc);
0b3e7a8b
AE
637 }
638
639 error = ovsdb_txn_commit(txn, durable_txn);
640 if (error) {
641 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
642 ovsdb_error_to_string(error));
643 }
0b3e7a8b
AE
644}
645
78876719 646/* Reconfigures ovsdb-server based on information in the database. */
0b1fae1b 647static void
78876719 648reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
b3c01ed3 649 const struct ovsdb *db, struct sset *remotes)
0b1fae1b
BP
650{
651 struct shash resolved_remotes;
b3c01ed3 652 const char *name;
0b1fae1b 653
78876719 654 /* Configure remotes. */
0b1fae1b 655 shash_init(&resolved_remotes);
b3c01ed3 656 SSET_FOR_EACH (name, remotes) {
0b1fae1b
BP
657 if (!strncmp(name, "db:", 3)) {
658 query_db_remotes(name, db, &resolved_remotes);
659 } else {
94db5407 660 add_remote(&resolved_remotes, name);
0b1fae1b
BP
661 }
662 }
663 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
88b64974 664 shash_destroy_free_data(&resolved_remotes);
0b1fae1b 665
78876719 666 /* Configure SSL. */
6f1e91b1
BP
667 stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
668 query_db_string(db, certificate_file));
78876719
BP
669 stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
670 bootstrap_ca_cert);
671}
0b1fae1b 672
aa78de9d 673static void
0e15264f
BP
674ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
675 const char *argv[] OVS_UNUSED,
aa78de9d
BP
676 void *exiting_)
677{
678 bool *exiting = exiting_;
679 *exiting = true;
bde9f75d 680 unixctl_command_reply(conn, NULL);
aa78de9d
BP
681}
682
ada496b5 683static void
0e15264f
BP
684ovsdb_server_compact(struct unixctl_conn *conn, int argc OVS_UNUSED,
685 const char *argv[] OVS_UNUSED, void *file_)
ada496b5
BP
686{
687 struct ovsdb_file *file = file_;
688 struct ovsdb_error *error;
689
690 VLOG_INFO("compacting database by user request");
691 error = ovsdb_file_compact(file);
692 if (!error) {
bde9f75d 693 unixctl_command_reply(conn, NULL);
ada496b5
BP
694 } else {
695 char *s = ovsdb_error_to_string(error);
696 ovsdb_error_destroy(error);
bde9f75d 697 unixctl_command_reply_error(conn, s);
ada496b5
BP
698 free(s);
699 }
700}
701
31d0b6c9
BP
702/* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
703 * connections and reconnect. */
704static void
0e15264f
BP
705ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
706 const char *argv[] OVS_UNUSED, void *jsonrpc_)
31d0b6c9
BP
707{
708 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
709
710 ovsdb_jsonrpc_server_reconnect(jsonrpc);
bde9f75d 711 unixctl_command_reply(conn, NULL);
31d0b6c9
BP
712}
713
f85f8ebb
BP
714static void
715parse_options(int argc, char *argv[], char **file_namep,
b3c01ed3 716 struct sset *remotes, char **unixctl_pathp,
475afa1b 717 char **run_command)
f85f8ebb
BP
718{
719 enum {
720 OPT_DUMMY = UCHAR_MAX + 1,
0b1fae1b 721 OPT_REMOTE,
aa78de9d 722 OPT_UNIXCTL,
475afa1b 723 OPT_RUN,
9467fe62 724 OPT_BOOTSTRAP_CA_CERT,
f85f8ebb 725 VLOG_OPTION_ENUMS,
8274ae95
BP
726 LEAK_CHECKER_OPTION_ENUMS,
727 DAEMON_OPTION_ENUMS
f85f8ebb
BP
728 };
729 static struct option long_options[] = {
e3c17733
BP
730 {"remote", required_argument, NULL, OPT_REMOTE},
731 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
732 {"run", required_argument, NULL, OPT_RUN},
733 {"help", no_argument, NULL, 'h'},
734 {"version", no_argument, NULL, 'V'},
f85f8ebb
BP
735 DAEMON_LONG_OPTIONS,
736 VLOG_LONG_OPTIONS,
737 LEAK_CHECKER_LONG_OPTIONS,
e3c17733
BP
738 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
739 {"private-key", required_argument, NULL, 'p'},
740 {"certificate", required_argument, NULL, 'c'},
741 {"ca-cert", required_argument, NULL, 'C'},
742 {NULL, 0, NULL, 0},
f85f8ebb
BP
743 };
744 char *short_options = long_options_to_short_options(long_options);
745
b3c01ed3 746 sset_init(remotes);
f85f8ebb
BP
747 for (;;) {
748 int c;
749
750 c = getopt_long(argc, argv, short_options, long_options, NULL);
751 if (c == -1) {
752 break;
753 }
754
755 switch (c) {
0b1fae1b 756 case OPT_REMOTE:
b3c01ed3 757 sset_add(remotes, optarg);
f85f8ebb
BP
758 break;
759
aa78de9d
BP
760 case OPT_UNIXCTL:
761 *unixctl_pathp = optarg;
762 break;
763
475afa1b
BP
764 case OPT_RUN:
765 *run_command = optarg;
766 break;
767
f85f8ebb
BP
768 case 'h':
769 usage();
770
771 case 'V':
55d5bb44 772 ovs_print_version(0, 0);
f85f8ebb
BP
773 exit(EXIT_SUCCESS);
774
775 VLOG_OPTION_HANDLERS
776 DAEMON_OPTION_HANDLERS
777 LEAK_CHECKER_OPTION_HANDLERS
778
78876719
BP
779 case 'p':
780 private_key_file = optarg;
781 break;
782
783 case 'c':
784 certificate_file = optarg;
785 break;
786
787 case 'C':
788 ca_cert_file = optarg;
789 bootstrap_ca_cert = false;
790 break;
9467fe62
BP
791
792 case OPT_BOOTSTRAP_CA_CERT:
78876719
BP
793 ca_cert_file = optarg;
794 bootstrap_ca_cert = true;
9467fe62 795 break;
9467fe62 796
f85f8ebb
BP
797 case '?':
798 exit(EXIT_FAILURE);
799
800 default:
801 abort();
802 }
803 }
804 free(short_options);
805
806 argc -= optind;
807 argv += optind;
808
29701194
BP
809 switch (argc) {
810 case 0:
f973f2af 811 *file_namep = xasprintf("%s/conf.db", ovs_dbdir());
29701194
BP
812 break;
813
814 case 1:
815 *file_namep = xstrdup(argv[0]);
816 break;
817
818 default:
f85f8ebb
BP
819 ovs_fatal(0, "database file is only non-option argument; "
820 "use --help for usage");
821 }
f85f8ebb
BP
822}
823
824static void
825usage(void)
826{
827 printf("%s: Open vSwitch database server\n"
828 "usage: %s [OPTIONS] DATABASE\n"
829 "where DATABASE is a database file in ovsdb format.\n",
830 program_name, program_name);
831 printf("\nJSON-RPC options (may be specified any number of times):\n"
0b1fae1b 832 " --remote=REMOTE connect or listen to REMOTE\n");
9467fe62 833 stream_usage("JSON-RPC", true, true, true);
f85f8ebb
BP
834 daemon_usage();
835 vlog_usage();
836 printf("\nOther options:\n"
475afa1b 837 " --run COMMAND run COMMAND as subprocess then exit\n"
7b38bdc8 838 " --unixctl=SOCKET override default control socket name\n"
f85f8ebb
BP
839 " -h, --help display this help message\n"
840 " -V, --version display version information\n");
841 leak_checker_usage();
842 exit(EXIT_SUCCESS);
843}