]> git.proxmox.com Git - mirror_ovs.git/blob - vswitchd/ovs-vswitchd.c
userspace: Log version on startup.
[mirror_ovs.git] / 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 VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
89
90 if (want_mlockall) {
91 #ifdef HAVE_MLOCKALL
92 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
93 VLOG_ERR("mlockall failed: %s", strerror(errno));
94 }
95 #else
96 VLOG_ERR("mlockall not supported on this system");
97 #endif
98 }
99
100 retval = unixctl_server_create(unixctl_path, &unixctl);
101 if (retval) {
102 exit(EXIT_FAILURE);
103 }
104 unixctl_command_register("exit", "", 0, 0, ovs_vswitchd_exit, &exiting);
105
106 bridge_init(remote);
107 free(remote);
108
109 exiting = false;
110 while (!exiting) {
111 if (signal_poll(sighup)) {
112 vlog_reopen_log_file();
113 }
114 memory_run();
115 if (memory_should_report()) {
116 struct simap usage;
117
118 simap_init(&usage);
119 bridge_get_memory_usage(&usage);
120 memory_report(&usage);
121 simap_destroy(&usage);
122 }
123 bridge_run_fast();
124 bridge_run();
125 bridge_run_fast();
126 unixctl_server_run(unixctl);
127 netdev_run();
128
129 signal_wait(sighup);
130 memory_wait();
131 bridge_wait();
132 unixctl_server_wait(unixctl);
133 netdev_wait();
134 if (exiting) {
135 poll_immediate_wake();
136 }
137 poll_block();
138 }
139 bridge_exit();
140 unixctl_server_destroy(unixctl);
141 signal_unregister(sighup);
142
143 return 0;
144 }
145
146 static char *
147 parse_options(int argc, char *argv[], char **unixctl_pathp)
148 {
149 enum {
150 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
151 OPT_MLOCKALL,
152 OPT_UNIXCTL,
153 VLOG_OPTION_ENUMS,
154 LEAK_CHECKER_OPTION_ENUMS,
155 OPT_BOOTSTRAP_CA_CERT,
156 OPT_ENABLE_DUMMY,
157 OPT_DISABLE_SYSTEM,
158 DAEMON_OPTION_ENUMS
159 };
160 static struct option long_options[] = {
161 {"help", no_argument, NULL, 'h'},
162 {"version", no_argument, NULL, 'V'},
163 {"mlockall", no_argument, NULL, OPT_MLOCKALL},
164 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
165 DAEMON_LONG_OPTIONS,
166 VLOG_LONG_OPTIONS,
167 LEAK_CHECKER_LONG_OPTIONS,
168 STREAM_SSL_LONG_OPTIONS,
169 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
170 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
171 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
172 {"disable-system", no_argument, NULL, OPT_DISABLE_SYSTEM},
173 {NULL, 0, NULL, 0},
174 };
175 char *short_options = long_options_to_short_options(long_options);
176
177 for (;;) {
178 int c;
179
180 c = getopt_long(argc, argv, short_options, long_options, NULL);
181 if (c == -1) {
182 break;
183 }
184
185 switch (c) {
186 case 'h':
187 usage();
188
189 case 'V':
190 ovs_print_version(OFP10_VERSION, OFP10_VERSION);
191 exit(EXIT_SUCCESS);
192
193 case OPT_MLOCKALL:
194 want_mlockall = true;
195 break;
196
197 case OPT_UNIXCTL:
198 *unixctl_pathp = optarg;
199 break;
200
201 VLOG_OPTION_HANDLERS
202 DAEMON_OPTION_HANDLERS
203 LEAK_CHECKER_OPTION_HANDLERS
204 STREAM_SSL_OPTION_HANDLERS
205
206 case OPT_PEER_CA_CERT:
207 stream_ssl_set_peer_ca_cert_file(optarg);
208 break;
209
210 case OPT_BOOTSTRAP_CA_CERT:
211 stream_ssl_set_ca_cert_file(optarg, true);
212 break;
213
214 case OPT_ENABLE_DUMMY:
215 dummy_enable(optarg && !strcmp(optarg, "override"));
216 break;
217
218 case OPT_DISABLE_SYSTEM:
219 dp_blacklist_provider("system");
220 break;
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 switch (argc) {
235 case 0:
236 return xasprintf("unix:%s/db.sock", ovs_rundir());
237
238 case 1:
239 return xstrdup(argv[0]);
240
241 default:
242 VLOG_FATAL("at most one non-option argument accepted; "
243 "use --help for usage");
244 }
245 }
246
247 static void
248 usage(void)
249 {
250 printf("%s: Open vSwitch daemon\n"
251 "usage: %s [OPTIONS] [DATABASE]\n"
252 "where DATABASE is a socket on which ovsdb-server is listening\n"
253 " (default: \"unix:%s/db.sock\").\n",
254 program_name, program_name, ovs_rundir());
255 stream_usage("DATABASE", true, false, true);
256 daemon_usage();
257 vlog_usage();
258 printf("\nOther options:\n"
259 " --unixctl=SOCKET override default control socket name\n"
260 " -h, --help display this help message\n"
261 " -V, --version display version information\n");
262 leak_checker_usage();
263 exit(EXIT_SUCCESS);
264 }
265
266 static void
267 ovs_vswitchd_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
268 const char *argv[] OVS_UNUSED, void *exiting_)
269 {
270 bool *exiting = exiting_;
271 *exiting = true;
272 unixctl_command_reply(conn, NULL);
273 }