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