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