]> git.proxmox.com Git - mirror_ovs.git/blob - ovn/controller-vtep/ovn-controller-vtep.c
Expose missing --peer-ca-cert and SSL options in usage and manpages.
[mirror_ovs.git] / ovn / controller-vtep / ovn-controller-vtep.c
1 /* Copyright (c) 2015, 2016 Nicira, Inc.
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
18 #include <errno.h>
19 #include <getopt.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "command-line.h"
25 #include "compiler.h"
26 #include "daemon.h"
27 #include "dirs.h"
28 #include "openvswitch/dynamic-string.h"
29 #include "fatal-signal.h"
30 #include "openvswitch/poll-loop.h"
31 #include "stream.h"
32 #include "stream-ssl.h"
33 #include "unixctl.h"
34 #include "util.h"
35 #include "openvswitch/vconn.h"
36 #include "openvswitch/vlog.h"
37 #include "ovn/lib/ovn-sb-idl.h"
38 #include "ovn/lib/ovn-util.h"
39 #include "vtep/vtep-idl.h"
40
41 #include "binding.h"
42 #include "gateway.h"
43 #include "vtep.h"
44 #include "ovn-controller-vtep.h"
45
46 static unixctl_cb_func ovn_controller_vtep_exit;
47
48 static void parse_options(int argc, char *argv[]);
49 OVS_NO_RETURN static void usage(void);
50
51 static char *vtep_remote;
52 static char *ovnsb_remote;
53 static char *default_db_;
54
55 int
56 main(int argc, char *argv[])
57 {
58 struct unixctl_server *unixctl;
59 bool exiting;
60 int retval;
61
62 ovs_cmdl_proctitle_init(argc, argv);
63 set_program_name(argv[0]);
64 service_start(&argc, &argv);
65 parse_options(argc, argv);
66 fatal_ignore_sigpipe();
67
68 daemonize_start(false);
69
70 retval = unixctl_server_create(NULL, &unixctl);
71 if (retval) {
72 exit(EXIT_FAILURE);
73 }
74 unixctl_command_register("exit", "", 0, 0, ovn_controller_vtep_exit,
75 &exiting);
76
77 daemonize_complete();
78
79 /* Connect to VTEP database. */
80 struct ovsdb_idl_loop vtep_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
81 ovsdb_idl_create(vtep_remote, &vteprec_idl_class, true, true));
82 ovsdb_idl_get_initial_snapshot(vtep_idl_loop.idl);
83
84 /* Connect to OVN SB database. */
85 struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
86 ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true));
87 ovsdb_idl_get_initial_snapshot(ovnsb_idl_loop.idl);
88
89 /* Main loop. */
90 exiting = false;
91 while (!exiting) {
92 struct controller_vtep_ctx ctx = {
93 .vtep_idl = vtep_idl_loop.idl,
94 .vtep_idl_txn = ovsdb_idl_loop_run(&vtep_idl_loop),
95 .ovnsb_idl = ovnsb_idl_loop.idl,
96 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
97 };
98
99 gateway_run(&ctx);
100 binding_run(&ctx);
101 vtep_run(&ctx);
102 unixctl_server_run(unixctl);
103
104 unixctl_server_wait(unixctl);
105 if (exiting) {
106 poll_immediate_wake();
107 }
108 ovsdb_idl_loop_commit_and_wait(&vtep_idl_loop);
109 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
110 poll_block();
111 if (should_service_stop()) {
112 exiting = true;
113 }
114 }
115
116 /* It's time to exit. Clean up the databases. */
117 bool done = false;
118 while (!done) {
119 struct controller_vtep_ctx ctx = {
120 .vtep_idl = vtep_idl_loop.idl,
121 .vtep_idl_txn = ovsdb_idl_loop_run(&vtep_idl_loop),
122 .ovnsb_idl = ovnsb_idl_loop.idl,
123 .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
124 };
125
126 /* Run all of the cleanup functions, even if one of them returns false.
127 * We're done if all of them return true. */
128 done = binding_cleanup(&ctx);
129 done = gateway_cleanup(&ctx) && done;
130 done = vtep_cleanup(&ctx) && done;
131 if (done) {
132 poll_immediate_wake();
133 }
134
135 ovsdb_idl_loop_commit_and_wait(&vtep_idl_loop);
136 ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
137 poll_block();
138 }
139
140 unixctl_server_destroy(unixctl);
141
142 ovsdb_idl_loop_destroy(&vtep_idl_loop);
143 ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
144
145 free(ovnsb_remote);
146 free(vtep_remote);
147 free(default_db_);
148 service_stop();
149
150 exit(retval);
151 }
152
153 static const char *
154 default_db(void)
155 {
156 if (!default_db_) {
157 default_db_ = xasprintf("unix:%s/db.sock", ovs_rundir());
158 }
159 return default_db_;
160 }
161
162 static void
163 parse_options(int argc, char *argv[])
164 {
165 enum {
166 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
167 OPT_BOOTSTRAP_CA_CERT,
168 VLOG_OPTION_ENUMS,
169 DAEMON_OPTION_ENUMS,
170 SSL_OPTION_ENUMS,
171 };
172
173 static struct option long_options[] = {
174 {"ovnsb-db", required_argument, NULL, 'd'},
175 {"vtep-db", required_argument, NULL, 'D'},
176 {"help", no_argument, NULL, 'h'},
177 {"version", no_argument, NULL, 'V'},
178 VLOG_LONG_OPTIONS,
179 DAEMON_LONG_OPTIONS,
180 STREAM_SSL_LONG_OPTIONS,
181 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
182 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
183 {NULL, 0, NULL, 0}
184 };
185 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
186
187 for (;;) {
188 int c;
189
190 c = getopt_long(argc, argv, short_options, long_options, NULL);
191 if (c == -1) {
192 break;
193 }
194
195 switch (c) {
196 case 'd':
197 ovnsb_remote = xstrdup(optarg);
198 break;
199
200 case 'D':
201 vtep_remote = xstrdup(optarg);
202 break;
203
204 case 'h':
205 usage();
206
207 case 'V':
208 ovs_print_version(OFP13_VERSION, OFP13_VERSION);
209 exit(EXIT_SUCCESS);
210
211 VLOG_OPTION_HANDLERS
212 DAEMON_OPTION_HANDLERS
213 STREAM_SSL_OPTION_HANDLERS
214
215 case OPT_PEER_CA_CERT:
216 stream_ssl_set_peer_ca_cert_file(optarg);
217 break;
218
219 case OPT_BOOTSTRAP_CA_CERT:
220 stream_ssl_set_ca_cert_file(optarg, true);
221 break;
222
223 case '?':
224 exit(EXIT_FAILURE);
225
226 default:
227 abort();
228 }
229 }
230 free(short_options);
231
232 if (!ovnsb_remote) {
233 ovnsb_remote = xstrdup(default_sb_db());
234 }
235
236 if (!vtep_remote) {
237 vtep_remote = xstrdup(default_db());
238 }
239 }
240
241 static void
242 usage(void)
243 {
244 printf("\
245 %s: OVN controller VTEP\n\
246 usage %s [OPTIONS]\n\
247 \n\
248 Options:\n\
249 --vtep-db=DATABASE connect to vtep database at DATABASE\n\
250 (default: %s)\n\
251 --ovnsb-db=DATABASE connect to ovn-sb database at DATABASE\n\
252 (default: %s)\n\
253 -h, --help display this help message\n\
254 -o, --options list available options\n\
255 -V, --version display version information\n\
256 ", program_name, program_name, default_db(), default_db());
257 stream_usage("database", true, false, true);
258 daemon_usage();
259 vlog_usage();
260 exit(EXIT_SUCCESS);
261 }
262
263 \f
264 static void
265 ovn_controller_vtep_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
266 const char *argv[] OVS_UNUSED, void *exiting_)
267 {
268 bool *exiting = exiting_;
269 *exiting = true;
270
271 unixctl_command_reply(conn, NULL);
272 }