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