Merge remote branch 'origin/master' into wdp-merge
[sliver-openvswitch.git] / vswitchd / ovs-vswitchd.c
1 /* Copyright (c) 2008, 2009, 2010 Nicira Networks
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 <assert.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_MLOCKALL
26 #include <sys/mman.h>
27 #endif
28
29 #include "bridge.h"
30 #include "command-line.h"
31 #include "compiler.h"
32 #include "daemon.h"
33 #include "leak-checker.h"
34 #include "netdev.h"
35 #include "ofproto/wdp.h"
36 #include "ovsdb-idl.h"
37 #include "poll-loop.h"
38 #include "proc-net-compat.h"
39 #include "process.h"
40 #include "signals.h"
41 #include "stream-ssl.h"
42 #include "stream.h"
43 #include "svec.h"
44 #include "timeval.h"
45 #include "unixctl.h"
46 #include "util.h"
47 #include "vconn.h"
48 #include "vswitchd/vswitch-idl.h"
49 #include "xfif.h"
50
51 #include "vlog.h"
52 #define THIS_MODULE VLM_vswitchd
53
54 static unixctl_cb_func ovs_vswitchd_exit;
55
56 static const char *parse_options(int argc, char *argv[]);
57 static void usage(void) NO_RETURN;
58
59 int
60 main(int argc, char *argv[])
61 {
62     struct unixctl_server *unixctl;
63     struct signal *sighup;
64     struct ovsdb_idl *idl;
65     const char *remote;
66     bool need_reconfigure;
67     bool inited, exiting;
68     unsigned int idl_seqno;
69     int retval;
70
71     proctitle_init(argc, argv);
72     set_program_name(argv[0]);
73     time_init();
74     vlog_init();
75     remote = parse_options(argc, argv);
76     signal(SIGPIPE, SIG_IGN);
77     sighup = signal_register(SIGHUP);
78     process_init();
79     ovsrec_init();
80
81     die_if_already_running();
82     daemonize_start();
83
84     retval = unixctl_server_create(NULL, &unixctl);
85     if (retval) {
86         exit(EXIT_FAILURE);
87     }
88     unixctl_command_register("exit", ovs_vswitchd_exit, &exiting);
89
90     daemonize_complete();
91
92     idl = ovsdb_idl_create(remote, &ovsrec_idl_class);
93     idl_seqno = ovsdb_idl_get_seqno(idl);
94
95     need_reconfigure = false;
96     inited = false;
97     exiting = false;
98     while (!exiting) {
99         if (signal_poll(sighup)) {
100             vlog_reopen_log_file();
101         }
102         if (inited && bridge_run()) {
103             need_reconfigure = true;
104         }
105         ovsdb_idl_run(idl);
106         if (idl_seqno != ovsdb_idl_get_seqno(idl)) {
107             idl_seqno = ovsdb_idl_get_seqno(idl);
108             need_reconfigure = true;
109         }
110         if (need_reconfigure) {
111             const struct ovsrec_open_vswitch *cfg;
112
113             need_reconfigure = false;
114             cfg = ovsrec_open_vswitch_first(idl);
115             if (cfg) {
116                 if (inited) {
117                     bridge_reconfigure(cfg);
118                 } else {
119                     bridge_init(cfg);
120                     inited = true;
121                 }
122             }
123         }
124         unixctl_server_run(unixctl);
125         wdp_run();
126         netdev_run();
127
128         signal_wait(sighup);
129         if (inited) {
130             bridge_wait();
131         }
132         ovsdb_idl_wait(idl);
133         unixctl_server_wait(unixctl);
134         wdp_wait();
135         netdev_wait();
136         poll_block();
137     }
138
139     return 0;
140 }
141
142 static const char *
143 parse_options(int argc, char *argv[])
144 {
145     enum {
146         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
147         OPT_MLOCKALL,
148         OPT_FAKE_PROC_NET,
149         VLOG_OPTION_ENUMS,
150         LEAK_CHECKER_OPTION_ENUMS,
151         OPT_BOOTSTRAP_CA_CERT
152     };
153     static struct option long_options[] = {
154         {"help",        no_argument, 0, 'h'},
155         {"version",     no_argument, 0, 'V'},
156         {"mlockall",    no_argument, 0, OPT_MLOCKALL},
157         {"fake-proc-net", no_argument, 0, OPT_FAKE_PROC_NET},
158         DAEMON_LONG_OPTIONS,
159         VLOG_LONG_OPTIONS,
160         LEAK_CHECKER_LONG_OPTIONS,
161 #ifdef HAVE_OPENSSL
162         STREAM_SSL_LONG_OPTIONS
163         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
164         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
165 #endif
166         {0, 0, 0, 0},
167     };
168     char *short_options = long_options_to_short_options(long_options);
169     int error;
170
171     for (;;) {
172         int c;
173
174         c = getopt_long(argc, argv, short_options, long_options, NULL);
175         if (c == -1) {
176             break;
177         }
178
179         switch (c) {
180         case 'H':
181         case 'h':
182             usage();
183
184         case 'V':
185             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
186             exit(EXIT_SUCCESS);
187
188         case OPT_MLOCKALL:
189 #ifdef HAVE_MLOCKALL
190             if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
191                 VLOG_ERR("mlockall failed: %s", strerror(errno));
192             }
193 #else
194             VLOG_ERR("mlockall not supported on this system");
195 #endif
196             break;
197
198         case OPT_FAKE_PROC_NET:
199             error = proc_net_compat_init();
200             if (error) {
201                 ovs_fatal(error, "failed to initialize /proc/net "
202                           "compatibility");
203             }
204             break;
205
206         VLOG_OPTION_HANDLERS
207         DAEMON_OPTION_HANDLERS
208         LEAK_CHECKER_OPTION_HANDLERS
209
210 #ifdef HAVE_OPENSSL
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 #endif
221
222         case '?':
223             exit(EXIT_FAILURE);
224
225         default:
226             abort();
227         }
228     }
229     free(short_options);
230
231     argc -= optind;
232     argv += optind;
233
234     if (argc != 1) {
235         ovs_fatal(0, "database socket is only non-option argument; "
236                 "use --help for usage");
237     }
238
239     return argv[0];
240 }
241
242 static void
243 usage(void)
244 {
245     printf("%s: Open vSwitch daemon\n"
246            "usage: %s [OPTIONS] DATABASE\n"
247            "where DATABASE is a socket on which ovsdb-server is listening.\n",
248            program_name, program_name);
249     stream_usage("DATABASE", true, false, true);
250     daemon_usage();
251     vlog_usage();
252     printf("\nLegacy compatibility options:\n"
253            " --fake-proc-net          simulate some files in /proc/net\n"
254            "\nOther options:\n"
255            "  -h, --help              display this help message\n"
256            "  -V, --version           display version information\n");
257     leak_checker_usage();
258     exit(EXIT_SUCCESS);
259 }
260
261 static void
262 ovs_vswitchd_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
263                   void *exiting_)
264 {
265     bool *exiting = exiting_;
266     *exiting = true;
267     unixctl_command_reply(conn, 200, NULL);
268 }