ovn: Add bootstrap options for OVN controllers.
[cascardo/ovs.git] / ovn / controller-vtep / ovn-controller-vtep.c
1 /* Copyright (c) 2015 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 "dynamic-string.h"
29 #include "fatal-signal.h"
30 #include "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 "vtep/vtep-idl.h"
39
40 #include "binding.h"
41 #include "gateway.h"
42 #include "ovn-controller-vtep.h"
43
44 static unixctl_cb_func ovn_controller_vtep_exit;
45
46 static void parse_options(int argc, char *argv[]);
47 OVS_NO_RETURN static void usage(void);
48
49 static char *vtep_remote;
50 static char *ovnsb_remote;
51 static char *default_db_;
52
53 int
54 main(int argc, char *argv[])
55 {
56     struct unixctl_server *unixctl;
57     bool exiting;
58     int retval;
59
60     ovs_cmdl_proctitle_init(argc, argv);
61     set_program_name(argv[0]);
62     service_start(&argc, &argv);
63     parse_options(argc, argv);
64     fatal_ignore_sigpipe();
65
66     daemonize_start();
67
68     retval = unixctl_server_create(NULL, &unixctl);
69     if (retval) {
70         exit(EXIT_FAILURE);
71     }
72     unixctl_command_register("exit", "", 0, 0, ovn_controller_vtep_exit,
73                              &exiting);
74
75     daemonize_complete();
76
77     vteprec_init();
78     sbrec_init();
79
80     /* Connect to VTEP database. */
81     struct ovsdb_idl_loop vtep_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
82         ovsdb_idl_create(vtep_remote, &vteprec_idl_class, true, true));
83     ovsdb_idl_get_initial_snapshot(vtep_idl_loop.idl);
84
85     /* Connect to OVN SB database. */
86     struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
87         ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true));
88     ovsdb_idl_get_initial_snapshot(ovnsb_idl_loop.idl);
89
90     /* Main loop. */
91     exiting = false;
92     while (!exiting) {
93         struct controller_vtep_ctx ctx = {
94             .vtep_idl = vtep_idl_loop.idl,
95             .vtep_idl_txn = ovsdb_idl_loop_run(&vtep_idl_loop),
96             .ovnsb_idl = ovnsb_idl_loop.idl,
97             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
98         };
99
100         gateway_run(&ctx);
101         binding_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         if (done) {
131             poll_immediate_wake();
132         }
133
134         ovsdb_idl_loop_commit_and_wait(&vtep_idl_loop);
135         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
136         poll_block();
137     }
138
139     unixctl_server_destroy(unixctl);
140
141     ovsdb_idl_loop_destroy(&vtep_idl_loop);
142     ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
143
144     free(ovnsb_remote);
145     free(vtep_remote);
146     free(default_db_);
147     service_stop();
148
149     exit(retval);
150 }
151
152 static const char *
153 default_db(void)
154 {
155     if (!default_db_) {
156         default_db_ = xasprintf("unix:%s/db.sock", ovs_rundir());
157     }
158     return default_db_;
159 }
160
161 static void
162 parse_options(int argc, char *argv[])
163 {
164     enum {
165         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
166         OPT_BOOTSTRAP_CA_CERT,
167         VLOG_OPTION_ENUMS,
168         DAEMON_OPTION_ENUMS
169     };
170
171     static struct option long_options[] = {
172         {"ovnsb-db", required_argument, NULL, 'd'},
173         {"vtep-db", required_argument, NULL, 'D'},
174         {"help", no_argument, NULL, 'h'},
175         {"version", no_argument, NULL, 'V'},
176         VLOG_LONG_OPTIONS,
177         DAEMON_LONG_OPTIONS,
178         STREAM_SSL_LONG_OPTIONS,
179         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
180         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
181         {NULL, 0, NULL, 0}
182     };
183     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
184
185     for (;;) {
186         int c;
187
188         c = getopt_long(argc, argv, short_options, long_options, NULL);
189         if (c == -1) {
190             break;
191         }
192
193         switch (c) {
194         case 'd':
195             ovnsb_remote = xstrdup(optarg);
196             break;
197
198         case 'D':
199             vtep_remote = xstrdup(optarg);
200             break;
201
202         case 'h':
203             usage();
204
205         case 'V':
206             ovs_print_version(OFP13_VERSION, OFP13_VERSION);
207             exit(EXIT_SUCCESS);
208
209         VLOG_OPTION_HANDLERS
210         DAEMON_OPTION_HANDLERS
211         STREAM_SSL_OPTION_HANDLERS
212
213         case OPT_PEER_CA_CERT:
214             stream_ssl_set_peer_ca_cert_file(optarg);
215             break;
216
217         case OPT_BOOTSTRAP_CA_CERT:
218             stream_ssl_set_ca_cert_file(optarg, true);
219             break;
220
221         case '?':
222             exit(EXIT_FAILURE);
223
224         default:
225             abort();
226         }
227     }
228     free(short_options);
229
230     argc -= optind;
231     argv += optind;
232
233     if (!ovnsb_remote) {
234         ovnsb_remote = xstrdup(default_db());
235     }
236
237     if (!vtep_remote) {
238         vtep_remote = xstrdup(default_db());
239     }
240 }
241
242 static void
243 usage(void)
244 {
245     printf("\
246 %s: OVN controller VTEP\n\
247 usage %s [OPTIONS]\n\
248 \n\
249 Options:\n\
250   --vtep-db=DATABASE        connect to vtep database at DATABASE\n\
251                             (default: %s)\n\
252   --ovnsb-db=DATABASE       connect to ovn-sb database at DATABASE\n\
253                             (default: %s)\n\
254   -h, --help                display this help message\n\
255   -o, --options             list available options\n\
256   -V, --version             display version information\n\
257 ", program_name, program_name, default_db(), default_db());
258     stream_usage("database", true, false, false);
259     daemon_usage();
260     vlog_usage();
261     exit(EXIT_SUCCESS);
262 }
263
264 \f
265 static void
266 ovn_controller_vtep_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
267                        const char *argv[] OVS_UNUSED, void *exiting_)
268 {
269     bool *exiting = exiting_;
270     *exiting = true;
271
272     unixctl_command_reply(conn, NULL);
273 }