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