ovn-northd: Ability to detach on Windows.
[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
52 int
53 main(int argc, char *argv[])
54 {
55     struct unixctl_server *unixctl;
56     bool exiting;
57     int retval;
58
59     ovs_cmdl_proctitle_init(argc, argv);
60     set_program_name(argv[0]);
61     service_start(&argc, &argv);
62     parse_options(argc, argv);
63     fatal_ignore_sigpipe();
64
65     daemonize_start();
66
67     retval = unixctl_server_create(NULL, &unixctl);
68     if (retval) {
69         exit(EXIT_FAILURE);
70     }
71     unixctl_command_register("exit", "", 0, 0, ovn_controller_vtep_exit,
72                              &exiting);
73
74     daemonize_complete();
75
76     vteprec_init();
77     sbrec_init();
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         unixctl_server_run(unixctl);
102
103         unixctl_server_wait(unixctl);
104         if (exiting) {
105             poll_immediate_wake();
106         }
107         ovsdb_idl_loop_commit_and_wait(&vtep_idl_loop);
108         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
109         poll_block();
110         if (should_service_stop()) {
111             exiting = true;
112         }
113     }
114
115     /* It's time to exit.  Clean up the databases. */
116     bool done = false;
117     while (!done) {
118         struct controller_vtep_ctx ctx = {
119             .vtep_idl = vtep_idl_loop.idl,
120             .vtep_idl_txn = ovsdb_idl_loop_run(&vtep_idl_loop),
121             .ovnsb_idl = ovnsb_idl_loop.idl,
122             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
123         };
124
125         /* Run all of the cleanup functions, even if one of them returns false.
126          * We're done if all of them return true. */
127         done = gateway_cleanup(&ctx);
128         done = binding_cleanup(&ctx) && done;
129         if (done) {
130             poll_immediate_wake();
131         }
132
133         ovsdb_idl_loop_commit_and_wait(&vtep_idl_loop);
134         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
135         poll_block();
136     }
137
138     unixctl_server_destroy(unixctl);
139
140     ovsdb_idl_loop_destroy(&vtep_idl_loop);
141     ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
142
143     free(ovnsb_remote);
144     free(vtep_remote);
145     service_stop();
146
147     exit(retval);
148 }
149
150 static char *
151 default_db(void)
152 {
153     static char *def;
154     if (!def) {
155         def = xasprintf("unix:%s/db.sock", ovs_rundir());
156     }
157     return def;
158 }
159
160 static void
161 parse_options(int argc, char *argv[])
162 {
163     enum {
164         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
165         VLOG_OPTION_ENUMS,
166         DAEMON_OPTION_ENUMS
167     };
168
169     static struct option long_options[] = {
170         {"ovnsb-db", required_argument, NULL, 'd'},
171         {"vtep-db", required_argument, NULL, 'D'},
172         {"help", no_argument, NULL, 'h'},
173         {"version", no_argument, NULL, 'V'},
174         VLOG_LONG_OPTIONS,
175         DAEMON_LONG_OPTIONS,
176         STREAM_SSL_LONG_OPTIONS,
177         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
178         {NULL, 0, NULL, 0}
179     };
180     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
181
182     for (;;) {
183         int c;
184
185         c = getopt_long(argc, argv, short_options, long_options, NULL);
186         if (c == -1) {
187             break;
188         }
189
190         switch (c) {
191         case 'd':
192             ovnsb_remote = optarg;
193             break;
194
195         case 'D':
196             vtep_remote = optarg;
197             break;
198
199         case 'h':
200             usage();
201
202         case 'V':
203             ovs_print_version(OFP13_VERSION, OFP13_VERSION);
204             exit(EXIT_SUCCESS);
205
206         VLOG_OPTION_HANDLERS
207         DAEMON_OPTION_HANDLERS
208         STREAM_SSL_OPTION_HANDLERS
209
210         case OPT_PEER_CA_CERT:
211             stream_ssl_set_peer_ca_cert_file(optarg);
212             break;
213
214         case '?':
215             exit(EXIT_FAILURE);
216
217         default:
218             abort();
219         }
220     }
221     free(short_options);
222
223     argc -= optind;
224     argv += optind;
225
226     if (!ovnsb_remote) {
227         ovnsb_remote = default_db();
228     }
229
230     if (!vtep_remote) {
231         vtep_remote = default_db();
232     }
233 }
234
235 static void
236 usage(void)
237 {
238     printf("\
239 %s: OVN controller VTEP\n\
240 usage %s [OPTIONS]\n\
241 \n\
242 Options:\n\
243   --vtep-db=DATABASE        connect to vtep database at DATABASE\n\
244                             (default: %s)\n\
245   --ovnsb-db=DATABASE       connect to ovn-sb database at DATABASE\n\
246                             (default: %s)\n\
247   -h, --help                display this help message\n\
248   -o, --options             list available options\n\
249   -V, --version             display version information\n\
250 ", program_name, program_name, default_db(), default_db());
251     stream_usage("database", true, false, false);
252     daemon_usage();
253     vlog_usage();
254     exit(EXIT_SUCCESS);
255 }
256
257 \f
258 static void
259 ovn_controller_vtep_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
260                        const char *argv[] OVS_UNUSED, void *exiting_)
261 {
262     bool *exiting = exiting_;
263     *exiting = true;
264
265     unixctl_command_reply(conn, NULL);
266 }