ovs-vswitchd: Get rid of call to process_init().
[sliver-openvswitch.git] / vswitchd / ovs-vswitchd.c
1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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 <limits.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #ifdef HAVE_MLOCKALL
25 #include <sys/mman.h>
26 #endif
27
28 #include "bridge.h"
29 #include "command-line.h"
30 #include "compiler.h"
31 #include "daemon.h"
32 #include "dirs.h"
33 #include "dpif.h"
34 #include "dummy.h"
35 #include "memory.h"
36 #include "netdev.h"
37 #include "openflow/openflow.h"
38 #include "ovsdb-idl.h"
39 #include "poll-loop.h"
40 #include "signals.h"
41 #include "simap.h"
42 #include "stream-ssl.h"
43 #include "stream.h"
44 #include "svec.h"
45 #include "timeval.h"
46 #include "unixctl.h"
47 #include "util.h"
48 #include "vconn.h"
49 #include "vlog.h"
50 #include "lib/vswitch-idl.h"
51
52 VLOG_DEFINE_THIS_MODULE(vswitchd);
53
54 /* --mlockall: If set, locks all process memory into physical RAM, preventing
55  * the kernel from paging any of its memory to disk. */
56 static bool want_mlockall;
57
58 static unixctl_cb_func ovs_vswitchd_exit;
59
60 static char *parse_options(int argc, char *argv[], char **unixctl_path);
61 static void usage(void) NO_RETURN;
62
63 int
64 main(int argc, char *argv[])
65 {
66     char *unixctl_path = NULL;
67     struct unixctl_server *unixctl;
68     struct signal *sighup;
69     char *remote;
70     bool exiting;
71     int retval;
72
73     proctitle_init(argc, argv);
74     set_program_name(argv[0]);
75     service_start(&argc, &argv);
76     remote = parse_options(argc, argv, &unixctl_path);
77     signal(SIGPIPE, SIG_IGN);
78     sighup = signal_register(SIGHUP);
79     ovsrec_init();
80
81     daemonize_start();
82
83     if (want_mlockall) {
84 #ifdef HAVE_MLOCKALL
85         if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
86             VLOG_ERR("mlockall failed: %s", ovs_strerror(errno));
87         }
88 #else
89         VLOG_ERR("mlockall not supported on this system");
90 #endif
91     }
92
93     retval = unixctl_server_create(unixctl_path, &unixctl);
94     if (retval) {
95         exit(EXIT_FAILURE);
96     }
97     unixctl_command_register("exit", "", 0, 0, ovs_vswitchd_exit, &exiting);
98
99     bridge_init(remote);
100     free(remote);
101
102     exiting = false;
103     while (!exiting) {
104         if (signal_poll(sighup)) {
105             vlog_reopen_log_file();
106         }
107         memory_run();
108         if (memory_should_report()) {
109             struct simap usage;
110
111             simap_init(&usage);
112             bridge_get_memory_usage(&usage);
113             memory_report(&usage);
114             simap_destroy(&usage);
115         }
116         bridge_run();
117         unixctl_server_run(unixctl);
118         netdev_run();
119
120         signal_wait(sighup);
121         memory_wait();
122         bridge_wait();
123         unixctl_server_wait(unixctl);
124         netdev_wait();
125         if (exiting) {
126             poll_immediate_wake();
127         }
128         poll_block();
129         if (should_service_stop()) {
130             exiting = true;
131         }
132     }
133     bridge_exit();
134     unixctl_server_destroy(unixctl);
135     service_stop();
136
137     return 0;
138 }
139
140 static char *
141 parse_options(int argc, char *argv[], char **unixctl_pathp)
142 {
143     enum {
144         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
145         OPT_MLOCKALL,
146         OPT_UNIXCTL,
147         VLOG_OPTION_ENUMS,
148         OPT_BOOTSTRAP_CA_CERT,
149         OPT_ENABLE_DUMMY,
150         OPT_DISABLE_SYSTEM,
151         DAEMON_OPTION_ENUMS
152     };
153     static const struct option long_options[] = {
154         {"help",        no_argument, NULL, 'h'},
155         {"version",     no_argument, NULL, 'V'},
156         {"mlockall",    no_argument, NULL, OPT_MLOCKALL},
157         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
158         DAEMON_LONG_OPTIONS,
159         VLOG_LONG_OPTIONS,
160         STREAM_SSL_LONG_OPTIONS,
161         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
162         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
163         {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
164         {"disable-system", no_argument, NULL, OPT_DISABLE_SYSTEM},
165         {NULL, 0, NULL, 0},
166     };
167     char *short_options = long_options_to_short_options(long_options);
168
169     for (;;) {
170         int c;
171
172         c = getopt_long(argc, argv, short_options, long_options, NULL);
173         if (c == -1) {
174             break;
175         }
176
177         switch (c) {
178         case 'h':
179             usage();
180
181         case 'V':
182             ovs_print_version(OFP10_VERSION, OFP10_VERSION);
183             exit(EXIT_SUCCESS);
184
185         case OPT_MLOCKALL:
186             want_mlockall = true;
187             break;
188
189         case OPT_UNIXCTL:
190             *unixctl_pathp = optarg;
191             break;
192
193         VLOG_OPTION_HANDLERS
194         DAEMON_OPTION_HANDLERS
195         STREAM_SSL_OPTION_HANDLERS
196
197         case OPT_PEER_CA_CERT:
198             stream_ssl_set_peer_ca_cert_file(optarg);
199             break;
200
201         case OPT_BOOTSTRAP_CA_CERT:
202             stream_ssl_set_ca_cert_file(optarg, true);
203             break;
204
205         case OPT_ENABLE_DUMMY:
206             dummy_enable(optarg && !strcmp(optarg, "override"));
207             break;
208
209         case OPT_DISABLE_SYSTEM:
210             dp_blacklist_provider("system");
211             break;
212
213         case '?':
214             exit(EXIT_FAILURE);
215
216         default:
217             abort();
218         }
219     }
220     free(short_options);
221
222     argc -= optind;
223     argv += optind;
224
225     switch (argc) {
226     case 0:
227         return xasprintf("unix:%s/db.sock", ovs_rundir());
228
229     case 1:
230         return xstrdup(argv[0]);
231
232     default:
233         VLOG_FATAL("at most one non-option argument accepted; "
234                    "use --help for usage");
235     }
236 }
237
238 static void
239 usage(void)
240 {
241     printf("%s: Open vSwitch daemon\n"
242            "usage: %s [OPTIONS] [DATABASE]\n"
243            "where DATABASE is a socket on which ovsdb-server is listening\n"
244            "      (default: \"unix:%s/db.sock\").\n",
245            program_name, program_name, ovs_rundir());
246     stream_usage("DATABASE", true, false, true);
247     daemon_usage();
248     vlog_usage();
249     printf("\nOther options:\n"
250            "  --unixctl=SOCKET        override default control socket name\n"
251            "  -h, --help              display this help message\n"
252            "  -V, --version           display version information\n");
253     exit(EXIT_SUCCESS);
254 }
255
256 static void
257 ovs_vswitchd_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
258                   const char *argv[] OVS_UNUSED, void *exiting_)
259 {
260     bool *exiting = exiting_;
261     *exiting = true;
262     unixctl_command_reply(conn, NULL);
263 }