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