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