]> git.proxmox.com Git - ovs.git/blame - ovsdb/ovsdb-server.c
datapath: make skb->csum consistent with rest of networking stack.
[ovs.git] / ovsdb / ovsdb-server.c
CommitLineData
b421d2af 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
f85f8ebb
BP
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <config.h>
17
f85f8ebb
BP
18#include <errno.h>
19#include <getopt.h>
798e1352 20#include <inttypes.h>
f85f8ebb 21#include <signal.h>
eb077b26 22#include <unistd.h>
f85f8ebb 23
0b1fae1b 24#include "column.h"
f85f8ebb
BP
25#include "command-line.h"
26#include "daemon.h"
29701194 27#include "dirs.h"
06834871 28#include "dummy.h"
b4e8d170 29#include "dynamic-string.h"
bd06962a 30#include "file.h"
da897f41 31#include "hash.h"
f85f8ebb
BP
32#include "json.h"
33#include "jsonrpc.h"
34#include "jsonrpc-server.h"
35#include "leak-checker.h"
36#include "list.h"
0d085684 37#include "memory.h"
da897f41 38#include "ovsdb.h"
0b1fae1b
BP
39#include "ovsdb-data.h"
40#include "ovsdb-types.h"
f85f8ebb
BP
41#include "ovsdb-error.h"
42#include "poll-loop.h"
43#include "process.h"
0b1fae1b 44#include "row.h"
0d085684 45#include "simap.h"
9467fe62 46#include "stream-ssl.h"
f85f8ebb 47#include "stream.h"
cc01d0bb 48#include "stress.h"
b3c01ed3 49#include "sset.h"
0b1fae1b 50#include "table.h"
f85f8ebb 51#include "timeval.h"
0b3e7a8b 52#include "transaction.h"
f85f8ebb
BP
53#include "trigger.h"
54#include "util.h"
55#include "unixctl.h"
f85f8ebb 56#include "vlog.h"
5136ce49 57
d98e6007 58VLOG_DEFINE_THIS_MODULE(ovsdb_server);
f85f8ebb 59
b4e8d170
BP
60struct db {
61 /* Initialized in main(). */
62 char *filename;
63 struct ovsdb_file *file;
64 struct ovsdb *db;
65
66 /* Only used by update_remote_status(). */
67 struct ovsdb_txn *txn;
68};
69
78876719
BP
70/* SSL configuration. */
71static char *private_key_file;
72static char *certificate_file;
73static char *ca_cert_file;
74static bool bootstrap_ca_cert;
75
aa78de9d 76static unixctl_cb_func ovsdb_server_exit;
ada496b5 77static unixctl_cb_func ovsdb_server_compact;
31d0b6c9 78static unixctl_cb_func ovsdb_server_reconnect;
aa78de9d 79
b421d2af
BP
80struct add_remote_aux {
81 struct sset *remotes;
82 struct db *dbs;
83 size_t n_dbs;
84};
85static unixctl_cb_func ovsdb_server_add_remote;
86static unixctl_cb_func ovsdb_server_remove_remote;
87static unixctl_cb_func ovsdb_server_list_remotes;
88
b4e8d170 89static void parse_options(int *argc, char **argvp[],
b3c01ed3 90 struct sset *remotes, char **unixctl_pathp,
475afa1b 91 char **run_command);
f85f8ebb
BP
92static void usage(void) NO_RETURN;
93
78876719 94static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
b4e8d170
BP
95 const struct db dbs[], size_t n_dbs,
96 struct sset *remotes);
0b1fae1b 97
0b3e7a8b 98static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
b3c01ed3 99 const struct sset *remotes,
b4e8d170 100 struct db dbs[], size_t n_dbs);
0b3e7a8b 101
f85f8ebb
BP
102int
103main(int argc, char *argv[])
104{
aa78de9d 105 char *unixctl_path = NULL;
475afa1b 106 char *run_command = NULL;
f85f8ebb
BP
107 struct unixctl_server *unixctl;
108 struct ovsdb_jsonrpc_server *jsonrpc;
b3c01ed3 109 struct sset remotes;
475afa1b 110 struct process *run_process;
aa78de9d 111 bool exiting;
f85f8ebb 112 int retval;
0b3e7a8b 113 long long int status_timer = LLONG_MIN;
b421d2af 114 struct add_remote_aux add_remote_aux;
f85f8ebb 115
b4e8d170
BP
116 struct db *dbs;
117 int n_dbs;
118 int i;
119
40f0707c 120 proctitle_init(argc, argv);
f85f8ebb 121 set_program_name(argv[0]);
cc01d0bb 122 stress_init_command();
f85f8ebb
BP
123 signal(SIGPIPE, SIG_IGN);
124 process_init();
125
b4e8d170 126 parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
f85f8ebb 127
95440284 128 daemonize_start();
eb077b26 129
b4e8d170
BP
130 n_dbs = MAX(1, argc);
131 dbs = xcalloc(n_dbs + 1, sizeof *dbs);
132 if (argc > 0) {
133 for (i = 0; i < argc; i++) {
134 dbs[i].filename = argv[i];
135 }
136 } else {
137 dbs[0].filename = xasprintf("%s/conf.db", ovs_dbdir());
f85f8ebb
BP
138 }
139
b4e8d170
BP
140 for (i = 0; i < n_dbs; i++) {
141 struct ovsdb_error *error;
142
143 error = ovsdb_file_open(dbs[i].filename, false,
144 &dbs[i].db, &dbs[i].file);
145 if (error) {
146 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
147 }
148 }
149
150 jsonrpc = ovsdb_jsonrpc_server_create();
151 for (i = 0; i < n_dbs; i++) {
152 if (!ovsdb_jsonrpc_server_add_db(jsonrpc, dbs[i].db)) {
153 ovs_fatal(0, "%s: duplicate database name",
154 dbs[i].db->schema->name);
155 }
156 }
157 reconfigure_from_db(jsonrpc, dbs, n_dbs, &remotes);
f85f8ebb 158
aa78de9d 159 retval = unixctl_server_create(unixctl_path, &unixctl);
f85f8ebb 160 if (retval) {
4d12270a 161 exit(EXIT_FAILURE);
f85f8ebb
BP
162 }
163
475afa1b
BP
164 if (run_command) {
165 char *run_argv[4];
166
167 run_argv[0] = "/bin/sh";
168 run_argv[1] = "-c";
169 run_argv[2] = run_command;
170 run_argv[3] = NULL;
171
e1208bc4 172 retval = process_start(run_argv, &run_process);
475afa1b
BP
173 if (retval) {
174 ovs_fatal(retval, "%s: process failed to start", run_command);
175 }
176 } else {
177 run_process = NULL;
178 }
179
95440284 180 daemonize_complete();
aa78de9d 181
08b9b190
EJ
182 if (!run_command) {
183 /* ovsdb-server is usually a long-running process, in which case it
184 * makes plenty of sense to log the version, but --run makes
185 * ovsdb-server more like a command-line tool, so skip it. */
186 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
187 }
9dbc190c 188
0e15264f 189 unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
b4e8d170
BP
190 unixctl_command_register("ovsdb-server/compact", "", 0, 1,
191 ovsdb_server_compact, dbs);
0e15264f 192 unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
7ff2009a 193 ovsdb_server_reconnect, jsonrpc);
aa78de9d 194
b421d2af
BP
195 add_remote_aux.remotes = &remotes;
196 add_remote_aux.dbs = dbs;
197 add_remote_aux.n_dbs = n_dbs;
198 unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
199 ovsdb_server_add_remote, &add_remote_aux);
200 unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
201 ovsdb_server_remove_remote, &remotes);
202 unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
203 ovsdb_server_list_remotes, &remotes);
204
aa78de9d
BP
205 exiting = false;
206 while (!exiting) {
b4e8d170
BP
207 int i;
208
0d085684
BP
209 memory_run();
210 if (memory_should_report()) {
211 struct simap usage;
212
213 simap_init(&usage);
214 ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
b4e8d170
BP
215 for (i = 0; i < n_dbs; i++) {
216 ovsdb_get_memory_usage(dbs[i].db, &usage);
217 }
0d085684
BP
218 memory_report(&usage);
219 simap_destroy(&usage);
220 }
221
b421d2af
BP
222 /* Run unixctl_server_run() before reconfigure_from_db() because
223 * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
224 * the set of remotes that reconfigure_from_db() uses. */
225 unixctl_server_run(unixctl);
226
b4e8d170 227 reconfigure_from_db(jsonrpc, dbs, n_dbs, &remotes);
f85f8ebb 228 ovsdb_jsonrpc_server_run(jsonrpc);
b4e8d170
BP
229
230 for (i = 0; i < n_dbs; i++) {
231 ovsdb_trigger_run(dbs[i].db, time_msec());
232 }
57d90319
BP
233 if (run_process) {
234 process_run();
235 if (process_exited(run_process)) {
236 exiting = true;
237 }
475afa1b 238 }
f85f8ebb 239
0b3e7a8b
AE
240 /* update Manager status(es) every 5 seconds */
241 if (time_msec() >= status_timer) {
242 status_timer = time_msec() + 5000;
b4e8d170 243 update_remote_status(jsonrpc, &remotes, dbs, n_dbs);
0b3e7a8b
AE
244 }
245
0d085684 246 memory_wait();
f85f8ebb
BP
247 ovsdb_jsonrpc_server_wait(jsonrpc);
248 unixctl_server_wait(unixctl);
b4e8d170
BP
249 for (i = 0; i < n_dbs; i++) {
250 ovsdb_trigger_wait(dbs[i].db, time_msec());
251 }
475afa1b
BP
252 if (run_process) {
253 process_wait(run_process);
254 }
6d37aaf1
BP
255 if (exiting) {
256 poll_immediate_wake();
257 }
0b3e7a8b 258 poll_timer_wait_until(status_timer);
f85f8ebb
BP
259 poll_block();
260 }
23935e8b 261 ovsdb_jsonrpc_server_destroy(jsonrpc);
b4e8d170
BP
262 for (i = 0; i < n_dbs; i++) {
263 ovsdb_destroy(dbs[i].db);
264 }
b3c01ed3 265 sset_destroy(&remotes);
23935e8b 266 unixctl_server_destroy(unixctl);
f85f8ebb 267
475afa1b
BP
268 if (run_process && process_exited(run_process)) {
269 int status = process_status(run_process);
270 if (status) {
271 ovs_fatal(0, "%s: child exited, %s",
272 run_command, process_status_msg(status));
273 }
274 }
275
f85f8ebb
BP
276 return 0;
277}
278
b4e8d170
BP
279static const struct db *
280find_db(const struct db dbs[], size_t n_dbs, const char *db_name)
281{
282 size_t i;
283
284 for (i = 0; i < n_dbs; i++) {
285 if (!strcmp(dbs[i].db->schema->name, db_name)) {
286 return &dbs[i];
287 }
288 }
289
290 return NULL;
291}
292
c02cf07b
BP
293static char * WARN_UNUSED_RESULT
294parse_db_column__(const struct db dbs[], size_t n_dbs,
295 const char *name_, char *name,
296 const struct db **dbp,
297 const struct ovsdb_table **tablep,
298 const struct ovsdb_column **columnp)
0b1fae1b 299{
b4e8d170 300 const char *table_name, *column_name;
0b1fae1b
BP
301 const struct ovsdb_column *column;
302 const struct ovsdb_table *table;
b4e8d170 303 const char *tokens[3];
0b1fae1b 304 char *save_ptr = NULL;
b4e8d170 305 const struct db *db;
0b1fae1b 306
c02cf07b
BP
307 *dbp = NULL;
308 *tablep = NULL;
309 *columnp = NULL;
310
b0ef0551 311 strtok_r(name, ":", &save_ptr); /* "db:" */
b4e8d170
BP
312 tokens[0] = strtok_r(NULL, ",", &save_ptr);
313 tokens[1] = strtok_r(NULL, ",", &save_ptr);
314 tokens[2] = strtok_r(NULL, ",", &save_ptr);
315 if (!tokens[0] || !tokens[1]) {
c02cf07b 316 return xasprintf("\"%s\": invalid syntax", name_);
0b1fae1b 317 }
b4e8d170
BP
318 if (tokens[2]) {
319 const char *db_name = tokens[0];
320 table_name = tokens[1];
321 column_name = tokens[2];
322
323 db = find_db(dbs, n_dbs, tokens[0]);
324 if (!db) {
c02cf07b 325 return xasprintf("\"%s\": no database named %s", name_, db_name);
b4e8d170
BP
326 }
327 } else {
328 if (n_dbs > 1) {
c02cf07b
BP
329 return xasprintf("\"%s\": database name must be specified "
330 "(because multiple databases are configured)",
331 name_);
b4e8d170
BP
332 }
333
334 table_name = tokens[0];
335 column_name = tokens[1];
336 db = &dbs[0];
337 }
0b1fae1b 338
b4e8d170 339 table = ovsdb_get_table(db->db, table_name);
0b1fae1b 340 if (!table) {
c02cf07b 341 return xasprintf("\"%s\": no table named %s", name_, table_name);
0b1fae1b
BP
342 }
343
344 column = ovsdb_table_schema_get_column(table->schema, column_name);
345 if (!column) {
c02cf07b
BP
346 return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
347 name_, table_name, column_name);
0b1fae1b
BP
348 }
349
b4e8d170 350 *dbp = db;
94db5407
BP
351 *columnp = column;
352 *tablep = table;
c02cf07b 353 return NULL;
94db5407
BP
354}
355
c02cf07b
BP
356/* Returns NULL if successful, otherwise a malloc()'d string describing the
357 * error. */
358static char * WARN_UNUSED_RESULT
359parse_db_column(const struct db dbs[], size_t n_dbs,
360 const char *name_,
361 const struct db **dbp,
362 const struct ovsdb_table **tablep,
363 const struct ovsdb_column **columnp)
364{
365 char *name = xstrdup(name_);
366 char *retval = parse_db_column__(dbs, n_dbs, name_, name,
367 dbp, tablep, columnp);
368 free(name);
369 return retval;
370}
371
372/* Returns NULL if successful, otherwise a malloc()'d string describing the
373 * error. */
374static char * WARN_UNUSED_RESULT
b4e8d170 375parse_db_string_column(const struct db dbs[], size_t n_dbs,
94db5407 376 const char *name,
b4e8d170 377 const struct db **dbp,
94db5407
BP
378 const struct ovsdb_table **tablep,
379 const struct ovsdb_column **columnp)
380{
c02cf07b 381 char *retval;
94db5407 382
c02cf07b
BP
383 retval = parse_db_column(dbs, n_dbs, name, dbp, tablep, columnp);
384 if (retval) {
385 return retval;
386 }
94db5407 387
c02cf07b
BP
388 if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
389 || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
390 return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
391 "not string or set of strings",
392 name, (*tablep)->schema->name, (*columnp)->name);
78876719
BP
393 }
394
c02cf07b 395 return NULL;
78876719
BP
396}
397
a02f8286 398static OVS_UNUSED const char *
b4e8d170 399query_db_string(const struct db dbs[], size_t n_dbs, const char *name)
78876719
BP
400{
401 if (!name || strncmp(name, "db:", 3)) {
402 return name;
403 } else {
404 const struct ovsdb_column *column;
405 const struct ovsdb_table *table;
406 const struct ovsdb_row *row;
b4e8d170 407 const struct db *db;
c02cf07b 408 char *retval;
78876719 409
c02cf07b
BP
410 retval = parse_db_string_column(dbs, n_dbs, name,
411 &db, &table, &column);
412 if (retval) {
413 ovs_fatal(0, "%s", retval);
414 }
78876719 415
4e8e4213 416 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
78876719
BP
417 const struct ovsdb_datum *datum;
418 size_t i;
419
420 datum = &row->fields[column->index];
421 for (i = 0; i < datum->n; i++) {
422 if (datum->keys[i].string[0]) {
423 return datum->keys[i].string;
424 }
425 }
426 }
427 return NULL;
0b1fae1b 428 }
78876719
BP
429}
430
94db5407
BP
431static struct ovsdb_jsonrpc_options *
432add_remote(struct shash *remotes, const char *target)
433{
434 struct ovsdb_jsonrpc_options *options;
435
436 options = shash_find_data(remotes, target);
437 if (!options) {
f1936eb6 438 options = ovsdb_jsonrpc_default_options(target);
94db5407
BP
439 shash_add(remotes, target, options);
440 }
441
442 return options;
443}
444
0b3e7a8b
AE
445static struct ovsdb_datum *
446get_datum(struct ovsdb_row *row, const char *column_name,
447 const enum ovsdb_atomic_type key_type,
448 const enum ovsdb_atomic_type value_type,
449 const size_t n_max)
94db5407
BP
450{
451 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
452 const struct ovsdb_table_schema *schema = row->table->schema;
453 const struct ovsdb_column *column;
94db5407
BP
454
455 column = ovsdb_table_schema_get_column(schema, column_name);
456 if (!column) {
457 VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
458 schema->name, column_name);
0b3e7a8b 459 return NULL;
94db5407
BP
460 }
461
0b3e7a8b
AE
462 if (column->type.key.type != key_type
463 || column->type.value.type != value_type
464 || column->type.n_max != n_max) {
94db5407
BP
465 if (!VLOG_DROP_DBG(&rl)) {
466 char *type_name = ovsdb_type_to_english(&column->type);
467 VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
0b3e7a8b
AE
468 "key type %s, value type %s, max elements %zd.",
469 schema->name, column_name, type_name,
470 ovsdb_atomic_type_to_string(key_type),
471 ovsdb_atomic_type_to_string(value_type),
472 n_max);
473 free(type_name);
94db5407 474 }
0b3e7a8b 475 return NULL;
94db5407
BP
476 }
477
0b3e7a8b
AE
478 return &row->fields[column->index];
479}
480
cea15768
EJ
481/* Read string-string key-values from a map. Returns the value associated with
482 * 'key', if found, or NULL */
483static const char *
f125905c 484read_map_string_column(const struct ovsdb_row *row, const char *column_name,
cea15768 485 const char *key)
f125905c
MM
486{
487 const struct ovsdb_datum *datum;
488 union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
489 size_t i;
490
ebc56baa
BP
491 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
492 OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
f125905c
MM
493
494 if (!datum) {
cea15768 495 return NULL;
f125905c
MM
496 }
497
498 for (i = 0; i < datum->n; i++) {
499 atom_key = &datum->keys[i];
500 if (!strcmp(atom_key->string, key)){
501 atom_value = &datum->values[i];
502 break;
503 }
504 }
505
cea15768 506 return atom_value ? atom_value->string : NULL;
f125905c
MM
507}
508
0b3e7a8b
AE
509static const union ovsdb_atom *
510read_column(const struct ovsdb_row *row, const char *column_name,
511 enum ovsdb_atomic_type type)
512{
513 const struct ovsdb_datum *datum;
514
ebc56baa
BP
515 datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
516 OVSDB_TYPE_VOID, 1);
0b3e7a8b 517 return datum && datum->n ? datum->keys : NULL;
94db5407
BP
518}
519
520static bool
521read_integer_column(const struct ovsdb_row *row, const char *column_name,
522 long long int *integerp)
523{
524 const union ovsdb_atom *atom;
525
526 atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
527 *integerp = atom ? atom->integer : 0;
528 return atom != NULL;
529}
530
531static bool
532read_string_column(const struct ovsdb_row *row, const char *column_name,
533 const char **stringp)
534{
535 const union ovsdb_atom *atom;
536
537 atom = read_column(row, column_name, OVSDB_TYPE_STRING);
e3c17733 538 *stringp = atom ? atom->string : NULL;
94db5407
BP
539 return atom != NULL;
540}
541
0b3e7a8b
AE
542static void
543write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
544{
c824c8a3
BP
545 const struct ovsdb_column *column;
546 struct ovsdb_datum *datum;
0b3e7a8b 547
c824c8a3
BP
548 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
549 datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
550 OVSDB_TYPE_VOID, 1);
0b3e7a8b
AE
551 if (!datum) {
552 return;
553 }
c824c8a3
BP
554
555 if (datum->n != 1) {
556 ovsdb_datum_destroy(datum, &column->type);
557
558 datum->n = 1;
559 datum->keys = xmalloc(sizeof *datum->keys);
560 datum->values = NULL;
561 }
562
0b3e7a8b
AE
563 datum->keys[0].boolean = value;
564}
565
566static void
567write_string_string_column(struct ovsdb_row *row, const char *column_name,
568 char **keys, char **values, size_t n)
569{
570 const struct ovsdb_column *column;
571 struct ovsdb_datum *datum;
572 size_t i;
573
574 column = ovsdb_table_schema_get_column(row->table->schema, column_name);
575 datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
576 UINT_MAX);
577 if (!datum) {
bfe834eb
BP
578 for (i = 0; i < n; i++) {
579 free(keys[i]);
580 free(values[i]);
581 }
0b3e7a8b
AE
582 return;
583 }
584
585 /* Free existing data. */
586 ovsdb_datum_destroy(datum, &column->type);
587
588 /* Allocate space for new values. */
589 datum->n = n;
590 datum->keys = xmalloc(n * sizeof *datum->keys);
591 datum->values = xmalloc(n * sizeof *datum->values);
592
593 for (i = 0; i < n; ++i) {
594 datum->keys[i].string = keys[i];
595 datum->values[i].string = values[i];
596 }
597
598 /* Sort and check constraints. */
599 ovsdb_datum_sort_assert(datum, column->type.key.type);
600}
601
94db5407
BP
602/* Adds a remote and options to 'remotes', based on the Manager table row in
603 * 'row'. */
604static void
605add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
606{
607 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
608 struct ovsdb_jsonrpc_options *options;
609 long long int max_backoff, probe_interval;
cea15768 610 const char *target, *dscp_string;
94db5407
BP
611
612 if (!read_string_column(row, "target", &target) || !target) {
613 VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
614 row->table->schema->name);
615 return;
616 }
617
618 options = add_remote(remotes, target);
619 if (read_integer_column(row, "max_backoff", &max_backoff)) {
620 options->max_backoff = max_backoff;
621 }
f441c1a8 622 if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
94db5407
BP
623 options->probe_interval = probe_interval;
624 }
f125905c 625
cea15768
EJ
626 options->dscp = DSCP_DEFAULT;
627 dscp_string = read_map_string_column(row, "other_config", "dscp");
628 if (dscp_string) {
629 int dscp = atoi(dscp_string);
630 if (dscp >= 0 && dscp <= 63) {
631 options->dscp = dscp;
632 }
633 }
94db5407
BP
634}
635
78876719 636static void
b4e8d170 637query_db_remotes(const char *name, const struct db dbs[], size_t n_dbs,
78876719
BP
638 struct shash *remotes)
639{
640 const struct ovsdb_column *column;
641 const struct ovsdb_table *table;
642 const struct ovsdb_row *row;
b4e8d170 643 const struct db *db;
c02cf07b 644 char *retval;
78876719 645
c02cf07b
BP
646 retval = parse_db_column(dbs, n_dbs, name, &db, &table, &column);
647 if (retval) {
648 ovs_fatal(0, "%s", retval);
649 }
0b1fae1b 650
94db5407
BP
651 if (column->type.key.type == OVSDB_TYPE_STRING
652 && column->type.value.type == OVSDB_TYPE_VOID) {
653 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
654 const struct ovsdb_datum *datum;
655 size_t i;
656
657 datum = &row->fields[column->index];
658 for (i = 0; i < datum->n; i++) {
659 add_remote(remotes, datum->keys[i].string);
660 }
661 }
662 } else if (column->type.key.type == OVSDB_TYPE_UUID
663 && column->type.key.u.uuid.refTable
664 && column->type.value.type == OVSDB_TYPE_VOID) {
665 const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
666 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
667 const struct ovsdb_datum *datum;
668 size_t i;
669
670 datum = &row->fields[column->index];
671 for (i = 0; i < datum->n; i++) {
672 const struct ovsdb_row *ref_row;
0b1fae1b 673
94db5407
BP
674 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
675 if (ref_row) {
676 add_manager_options(remotes, ref_row);
677 }
678 }
0b1fae1b
BP
679 }
680 }
681}
682
0b3e7a8b
AE
683static void
684update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
87fcbc60 685 const struct ovsdb_jsonrpc_server *jsonrpc)
0b3e7a8b 686{
87fcbc60 687 struct ovsdb_jsonrpc_remote_status status;
0b3e7a8b
AE
688 struct ovsdb_row *rw_row;
689 const char *target;
798e1352 690 char *keys[9], *values[9];
0b3e7a8b
AE
691 size_t n = 0;
692
693 /* Get the "target" (protocol/host/port) spec. */
694 if (!read_string_column(row, "target", &target)) {
695 /* Bad remote spec or incorrect schema. */
696 return;
697 }
0b3e7a8b 698 rw_row = ovsdb_txn_row_modify(txn, row);
87fcbc60 699 ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
0b3e7a8b
AE
700
701 /* Update status information columns. */
87fcbc60 702 write_bool_column(rw_row, "is_connected", status.is_connected);
0b3e7a8b 703
87fcbc60
BP
704 if (status.state) {
705 keys[n] = xstrdup("state");
706 values[n++] = xstrdup(status.state);
707 }
708 if (status.sec_since_connect != UINT_MAX) {
5eda645e 709 keys[n] = xstrdup("sec_since_connect");
87fcbc60 710 values[n++] = xasprintf("%u", status.sec_since_connect);
5eda645e 711 }
87fcbc60 712 if (status.sec_since_disconnect != UINT_MAX) {
5eda645e 713 keys[n] = xstrdup("sec_since_disconnect");
87fcbc60 714 values[n++] = xasprintf("%u", status.sec_since_disconnect);
5eda645e 715 }
87fcbc60 716 if (status.last_error) {
0b3e7a8b
AE
717 keys[n] = xstrdup("last_error");
718 values[n++] =
87fcbc60 719 xstrdup(ovs_retval_to_string(status.last_error));
0b3e7a8b 720 }
da897f41
BP
721 if (status.locks_held && status.locks_held[0]) {
722 keys[n] = xstrdup("locks_held");
723 values[n++] = xstrdup(status.locks_held);
724 }
725 if (status.locks_waiting && status.locks_waiting[0]) {
726 keys[n] = xstrdup("locks_waiting");
727 values[n++] = xstrdup(status.locks_waiting);
728 }
729 if (status.locks_lost && status.locks_lost[0]) {
730 keys[n] = xstrdup("locks_lost");
731 values[n++] = xstrdup(status.locks_lost);
732 }
a11f6164
BP
733 if (status.n_connections > 1) {
734 keys[n] = xstrdup("n_connections");
735 values[n++] = xasprintf("%d", status.n_connections);
736 }
798e1352
BP
737 if (status.bound_port != htons(0)) {
738 keys[n] = xstrdup("bound_port");
739 values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
740 }
0b3e7a8b 741 write_string_string_column(rw_row, "status", keys, values, n);
da897f41
BP
742
743 ovsdb_jsonrpc_server_free_remote_status(&status);
0b3e7a8b
AE
744}
745
746static void
b4e8d170 747update_remote_rows(const struct db dbs[], size_t n_dbs,
87fcbc60
BP
748 const char *remote_name,
749 const struct ovsdb_jsonrpc_server *jsonrpc)
0b3e7a8b
AE
750{
751 const struct ovsdb_table *table, *ref_table;
752 const struct ovsdb_column *column;
753 const struct ovsdb_row *row;
b4e8d170 754 const struct db *db;
c02cf07b 755 char *retval;
0b3e7a8b
AE
756
757 if (strncmp("db:", remote_name, 3)) {
758 return;
759 }
760
c02cf07b
BP
761 retval = parse_db_column(dbs, n_dbs, remote_name, &db, &table, &column);
762 if (retval) {
763 ovs_fatal(0, "%s", retval);
764 }
0b3e7a8b
AE
765
766 if (column->type.key.type != OVSDB_TYPE_UUID
767 || !column->type.key.u.uuid.refTable
768 || column->type.value.type != OVSDB_TYPE_VOID) {
769 return;
770 }
771
772 ref_table = column->type.key.u.uuid.refTable;
773
774 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
775 const struct ovsdb_datum *datum;
776 size_t i;
777
778 datum = &row->fields[column->index];
779 for (i = 0; i < datum->n; i++) {
780 const struct ovsdb_row *ref_row;
781
782 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
783 if (ref_row) {
b4e8d170 784 update_remote_row(ref_row, db->txn, jsonrpc);
0b3e7a8b
AE
785 }
786 }
787 }
788}
789
790static void
791update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
b4e8d170
BP
792 const struct sset *remotes,
793 struct db dbs[], size_t n_dbs)
0b3e7a8b
AE
794{
795 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
b3c01ed3 796 const char *remote;
b4e8d170 797 size_t i;
0b3e7a8b 798
b4e8d170
BP
799 for (i = 0; i < n_dbs; i++) {
800 dbs[i].txn = ovsdb_txn_create(dbs[i].db);
801 }
0b3e7a8b
AE
802
803 /* Iterate over --remote arguments given on command line. */
b3c01ed3 804 SSET_FOR_EACH (remote, remotes) {
b4e8d170 805 update_remote_rows(dbs, n_dbs, remote, jsonrpc);
0b3e7a8b
AE
806 }
807
b4e8d170
BP
808 for (i = 0; i < n_dbs; i++) {
809 struct ovsdb_error *error = ovsdb_txn_commit(dbs[i].txn, false);
810 if (error) {
811 VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
812 ovsdb_error_to_string(error));
813 ovsdb_error_destroy(error);
814 }
0b3e7a8b 815 }
0b3e7a8b
AE
816}
817
78876719 818/* Reconfigures ovsdb-server based on information in the database. */
0b1fae1b 819static void
78876719 820reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
b4e8d170 821 const struct db dbs[], size_t n_dbs, struct sset *remotes)
0b1fae1b
BP
822{
823 struct shash resolved_remotes;
b3c01ed3 824 const char *name;
0b1fae1b 825
78876719 826 /* Configure remotes. */
0b1fae1b 827 shash_init(&resolved_remotes);
b3c01ed3 828 SSET_FOR_EACH (name, remotes) {
0b1fae1b 829 if (!strncmp(name, "db:", 3)) {
b4e8d170 830 query_db_remotes(name, dbs, n_dbs, &resolved_remotes);
0b1fae1b 831 } else {
94db5407 832 add_remote(&resolved_remotes, name);
0b1fae1b
BP
833 }
834 }
835 ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
88b64974 836 shash_destroy_free_data(&resolved_remotes);
0b1fae1b 837
78876719 838 /* Configure SSL. */
b4e8d170
BP
839 stream_ssl_set_key_and_cert(query_db_string(dbs, n_dbs, private_key_file),
840 query_db_string(dbs, n_dbs, certificate_file));
841 stream_ssl_set_ca_cert_file(query_db_string(dbs, n_dbs, ca_cert_file),
78876719
BP
842 bootstrap_ca_cert);
843}
0b1fae1b 844
aa78de9d 845static void
0e15264f
BP
846ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
847 const char *argv[] OVS_UNUSED,
aa78de9d
BP
848 void *exiting_)
849{
850 bool *exiting = exiting_;
851 *exiting = true;
bde9f75d 852 unixctl_command_reply(conn, NULL);
aa78de9d
BP
853}
854
ada496b5 855static void
b4e8d170
BP
856ovsdb_server_compact(struct unixctl_conn *conn, int argc,
857 const char *argv[], void *dbs_)
ada496b5 858{
b4e8d170
BP
859 struct db *dbs = dbs_;
860 struct ds reply;
861 struct db *db;
862 int n = 0;
ada496b5 863
b4e8d170
BP
864 ds_init(&reply);
865 for (db = dbs; db->filename != NULL; db++) {
866 const char *name = db->db->schema->name;
867
868 if (argc < 2 || !strcmp(argv[1], name)) {
869 struct ovsdb_error *error;
870
871 VLOG_INFO("compacting %s database by user request", name);
872
873 error = ovsdb_file_compact(db->file);
874 if (error) {
875 char *s = ovsdb_error_to_string(error);
876 ds_put_format(&reply, "%s\n", s);
877 free(s);
878 }
879
880 n++;
881 }
882 }
883
884 if (!n) {
885 unixctl_command_reply_error(conn, "no database by that name");
886 } else if (reply.length) {
887 unixctl_command_reply_error(conn, ds_cstr(&reply));
ada496b5 888 } else {
b4e8d170 889 unixctl_command_reply(conn, NULL);
ada496b5 890 }
b4e8d170 891 ds_destroy(&reply);
ada496b5
BP
892}
893
31d0b6c9
BP
894/* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
895 * connections and reconnect. */
896static void
0e15264f
BP
897ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
898 const char *argv[] OVS_UNUSED, void *jsonrpc_)
31d0b6c9
BP
899{
900 struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
901
902 ovsdb_jsonrpc_server_reconnect(jsonrpc);
bde9f75d 903 unixctl_command_reply(conn, NULL);
31d0b6c9
BP
904}
905
b421d2af
BP
906/* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
907 * ovsdb-server services. */
908static void
909ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
910 const char *argv[], void *aux_)
911{
912 struct add_remote_aux *aux = aux_;
913 const char *remote = argv[1];
914
915 const struct ovsdb_column *column;
916 const struct ovsdb_table *table;
917 const struct db *db;
918 char *retval;
919
920 retval = (strncmp("db:", remote, 3)
921 ? NULL
922 : parse_db_column(aux->dbs, aux->n_dbs, remote,
923 &db, &table, &column));
924 if (!retval) {
925 sset_add(aux->remotes, remote);
926 unixctl_command_reply(conn, NULL);
927 } else {
928 unixctl_command_reply_error(conn, retval);
929 free(retval);
930 }
931}
932
933/* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
934 * that ovsdb-server services. */
935static void
936ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
937 const char *argv[], void *remotes_)
938{
939 struct sset *remotes = remotes_;
940 struct sset_node *node;
941
942 node = sset_find(remotes, argv[1]);
943 if (node) {
944 sset_delete(remotes, node);
945 unixctl_command_reply(conn, NULL);
946 } else {
947 unixctl_command_reply_error(conn, "no such remote");
948 }
949}
950
951/* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
952static void
953ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
954 const char *argv[] OVS_UNUSED, void *remotes_)
955{
956 struct sset *remotes = remotes_;
957 const char **list, **p;
958 struct ds s;
959
960 ds_init(&s);
961
962 list = sset_sort(remotes);
963 for (p = list; *p; p++) {
964 ds_put_format(&s, "%s\n", *p);
965 }
966 free(list);
967
968 unixctl_command_reply(conn, ds_cstr(&s));
969 ds_destroy(&s);
970}
971
f85f8ebb 972static void
b4e8d170
BP
973parse_options(int *argcp, char **argvp[],
974 struct sset *remotes, char **unixctl_pathp, char **run_command)
f85f8ebb
BP
975{
976 enum {
06834871 977 OPT_REMOTE = UCHAR_MAX + 1,
aa78de9d 978 OPT_UNIXCTL,
475afa1b 979 OPT_RUN,
9467fe62 980 OPT_BOOTSTRAP_CA_CERT,
06834871 981 OPT_ENABLE_DUMMY,
f85f8ebb 982 VLOG_OPTION_ENUMS,
8274ae95
BP
983 LEAK_CHECKER_OPTION_ENUMS,
984 DAEMON_OPTION_ENUMS
f85f8ebb 985 };
07fc4ed3 986 static const struct option long_options[] = {
e3c17733
BP
987 {"remote", required_argument, NULL, OPT_REMOTE},
988 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
989 {"run", required_argument, NULL, OPT_RUN},
990 {"help", no_argument, NULL, 'h'},
991 {"version", no_argument, NULL, 'V'},
f85f8ebb
BP
992 DAEMON_LONG_OPTIONS,
993 VLOG_LONG_OPTIONS,
994 LEAK_CHECKER_LONG_OPTIONS,
e3c17733
BP
995 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
996 {"private-key", required_argument, NULL, 'p'},
997 {"certificate", required_argument, NULL, 'c'},
998 {"ca-cert", required_argument, NULL, 'C'},
06834871 999 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
e3c17733 1000 {NULL, 0, NULL, 0},
f85f8ebb
BP
1001 };
1002 char *short_options = long_options_to_short_options(long_options);
b4e8d170
BP
1003 int argc = *argcp;
1004 char **argv = *argvp;
f85f8ebb 1005
b3c01ed3 1006 sset_init(remotes);
f85f8ebb
BP
1007 for (;;) {
1008 int c;
1009
1010 c = getopt_long(argc, argv, short_options, long_options, NULL);
1011 if (c == -1) {
1012 break;
1013 }
1014
1015 switch (c) {
0b1fae1b 1016 case OPT_REMOTE:
b3c01ed3 1017 sset_add(remotes, optarg);
f85f8ebb
BP
1018 break;
1019
aa78de9d
BP
1020 case OPT_UNIXCTL:
1021 *unixctl_pathp = optarg;
1022 break;
1023
475afa1b
BP
1024 case OPT_RUN:
1025 *run_command = optarg;
1026 break;
1027
f85f8ebb
BP
1028 case 'h':
1029 usage();
1030
1031 case 'V':
55d5bb44 1032 ovs_print_version(0, 0);
f85f8ebb
BP
1033 exit(EXIT_SUCCESS);
1034
1035 VLOG_OPTION_HANDLERS
1036 DAEMON_OPTION_HANDLERS
1037 LEAK_CHECKER_OPTION_HANDLERS
1038
78876719
BP
1039 case 'p':
1040 private_key_file = optarg;
1041 break;
1042
1043 case 'c':
1044 certificate_file = optarg;
1045 break;
1046
1047 case 'C':
1048 ca_cert_file = optarg;
1049 bootstrap_ca_cert = false;
1050 break;
9467fe62
BP
1051
1052 case OPT_BOOTSTRAP_CA_CERT:
78876719
BP
1053 ca_cert_file = optarg;
1054 bootstrap_ca_cert = true;
9467fe62 1055 break;
9467fe62 1056
06834871
BP
1057 case OPT_ENABLE_DUMMY:
1058 dummy_enable(optarg && !strcmp(optarg, "override"));
1059 break;
1060
f85f8ebb
BP
1061 case '?':
1062 exit(EXIT_FAILURE);
1063
1064 default:
1065 abort();
1066 }
1067 }
1068 free(short_options);
1069
b4e8d170
BP
1070 *argcp -= optind;
1071 *argvp += optind;
f85f8ebb
BP
1072}
1073
1074static void
1075usage(void)
1076{
1077 printf("%s: Open vSwitch database server\n"
b4e8d170
BP
1078 "usage: %s [OPTIONS] [DATABASE...]\n"
1079 "where each DATABASE is a database file in ovsdb format.\n"
1080 "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1081 program_name, program_name, ovs_dbdir());
f85f8ebb 1082 printf("\nJSON-RPC options (may be specified any number of times):\n"
0b1fae1b 1083 " --remote=REMOTE connect or listen to REMOTE\n");
9467fe62 1084 stream_usage("JSON-RPC", true, true, true);
f85f8ebb
BP
1085 daemon_usage();
1086 vlog_usage();
1087 printf("\nOther options:\n"
475afa1b 1088 " --run COMMAND run COMMAND as subprocess then exit\n"
7b38bdc8 1089 " --unixctl=SOCKET override default control socket name\n"
f85f8ebb
BP
1090 " -h, --help display this help message\n"
1091 " -V, --version display version information\n");
1092 leak_checker_usage();
1093 exit(EXIT_SUCCESS);
1094}