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