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