]> git.proxmox.com Git - mirror_ovs.git/blob - vswitchd/ovs-vswitchd.c
vswitchd: Track status of memory locking.
[mirror_ovs.git] / vswitchd / ovs-vswitchd.c
1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 "fatal-signal.h"
36 #include "memory.h"
37 #include "netdev.h"
38 #include "openflow/openflow.h"
39 #include "ovsdb-idl.h"
40 #include "ovs-rcu.h"
41 #include "ovs-router.h"
42 #include "ovs-thread.h"
43 #include "openvswitch/poll-loop.h"
44 #include "simap.h"
45 #include "stream-ssl.h"
46 #include "stream.h"
47 #include "svec.h"
48 #include "timeval.h"
49 #include "unixctl.h"
50 #include "util.h"
51 #include "openvswitch/vconn.h"
52 #include "openvswitch/vlog.h"
53 #include "lib/vswitch-idl.h"
54 #include "lib/dns-resolve.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 OVS_NO_RETURN static void usage(void);
66
67 struct ovs_vswitchd_exit_args {
68 bool *exiting;
69 bool *cleanup;
70 };
71
72 int
73 main(int argc, char *argv[])
74 {
75 char *unixctl_path = NULL;
76 struct unixctl_server *unixctl;
77 char *remote;
78 bool exiting, cleanup;
79 struct ovs_vswitchd_exit_args exit_args = {&exiting, &cleanup};
80 int retval;
81
82 set_program_name(argv[0]);
83 ovsthread_id_init();
84
85 dns_resolve_init(true);
86 ovs_cmdl_proctitle_init(argc, argv);
87 service_start(&argc, &argv);
88 remote = parse_options(argc, argv, &unixctl_path);
89 fatal_ignore_sigpipe();
90
91 daemonize_start(true);
92
93 if (want_mlockall) {
94 #ifdef HAVE_MLOCKALL
95 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
96 VLOG_ERR("mlockall failed: %s", ovs_strerror(errno));
97 } else {
98 set_memory_locked();
99 }
100 #else
101 VLOG_ERR("mlockall not supported on this system");
102 #endif
103 }
104
105 retval = unixctl_server_create(unixctl_path, &unixctl);
106 if (retval) {
107 exit(EXIT_FAILURE);
108 }
109 unixctl_command_register("exit", "[--cleanup]", 0, 1,
110 ovs_vswitchd_exit, &exit_args);
111
112 bridge_init(remote);
113 free(remote);
114
115 exiting = false;
116 cleanup = false;
117 while (!exiting) {
118 memory_run();
119 if (memory_should_report()) {
120 struct simap usage;
121
122 simap_init(&usage);
123 bridge_get_memory_usage(&usage);
124 memory_report(&usage);
125 simap_destroy(&usage);
126 }
127 bridge_run();
128 unixctl_server_run(unixctl);
129 netdev_run();
130
131 memory_wait();
132 bridge_wait();
133 unixctl_server_wait(unixctl);
134 netdev_wait();
135 if (exiting) {
136 poll_immediate_wake();
137 }
138 poll_block();
139 if (should_service_stop()) {
140 exiting = true;
141 }
142 }
143 bridge_exit(cleanup);
144 unixctl_server_destroy(unixctl);
145 service_stop();
146 vlog_disable_async();
147 ovsrcu_exit();
148 dns_resolve_destroy();
149
150 return 0;
151 }
152
153 static char *
154 parse_options(int argc, char *argv[], char **unixctl_pathp)
155 {
156 enum {
157 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
158 OPT_MLOCKALL,
159 OPT_UNIXCTL,
160 VLOG_OPTION_ENUMS,
161 OPT_BOOTSTRAP_CA_CERT,
162 OPT_ENABLE_DUMMY,
163 OPT_DISABLE_SYSTEM,
164 DAEMON_OPTION_ENUMS,
165 OPT_DPDK,
166 SSL_OPTION_ENUMS,
167 OPT_DUMMY_NUMA,
168 };
169 static const struct option long_options[] = {
170 {"help", no_argument, NULL, 'h'},
171 {"version", no_argument, NULL, 'V'},
172 {"mlockall", no_argument, NULL, OPT_MLOCKALL},
173 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
174 DAEMON_LONG_OPTIONS,
175 VLOG_LONG_OPTIONS,
176 STREAM_SSL_LONG_OPTIONS,
177 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
178 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
179 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
180 {"disable-system", no_argument, NULL, OPT_DISABLE_SYSTEM},
181 {"dpdk", optional_argument, NULL, OPT_DPDK},
182 {"dummy-numa", required_argument, NULL, OPT_DUMMY_NUMA},
183 {NULL, 0, NULL, 0},
184 };
185 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
186
187 for (;;) {
188 int c;
189
190 c = getopt_long(argc, argv, short_options, long_options, NULL);
191 if (c == -1) {
192 break;
193 }
194
195 switch (c) {
196 case 'h':
197 usage();
198
199 case 'V':
200 ovs_print_version(0, 0);
201 print_dpdk_version();
202 exit(EXIT_SUCCESS);
203
204 case OPT_MLOCKALL:
205 want_mlockall = true;
206 break;
207
208 case OPT_UNIXCTL:
209 *unixctl_pathp = optarg;
210 break;
211
212 VLOG_OPTION_HANDLERS
213 DAEMON_OPTION_HANDLERS
214 STREAM_SSL_OPTION_HANDLERS
215
216 case OPT_PEER_CA_CERT:
217 stream_ssl_set_peer_ca_cert_file(optarg);
218 break;
219
220 case OPT_BOOTSTRAP_CA_CERT:
221 stream_ssl_set_ca_cert_file(optarg, true);
222 break;
223
224 case OPT_ENABLE_DUMMY:
225 dummy_enable(optarg);
226 break;
227
228 case OPT_DISABLE_SYSTEM:
229 dp_blacklist_provider("system");
230 ovs_router_disable_system_routing_table();
231 break;
232
233 case '?':
234 exit(EXIT_FAILURE);
235
236 case OPT_DPDK:
237 ovs_fatal(0, "Using --dpdk to configure DPDK is not supported.");
238 break;
239
240 case OPT_DUMMY_NUMA:
241 ovs_numa_set_dummy(optarg);
242 break;
243
244 default:
245 abort();
246 }
247 }
248 free(short_options);
249
250 argc -= optind;
251 argv += optind;
252
253 switch (argc) {
254 case 0:
255 return xasprintf("unix:%s/db.sock", ovs_rundir());
256
257 case 1:
258 return xstrdup(argv[0]);
259
260 default:
261 VLOG_FATAL("at most one non-option argument accepted; "
262 "use --help for usage");
263 }
264 }
265
266 static void
267 usage(void)
268 {
269 printf("%s: Open vSwitch daemon\n"
270 "usage: %s [OPTIONS] [DATABASE]\n"
271 "where DATABASE is a socket on which ovsdb-server is listening\n"
272 " (default: \"unix:%s/db.sock\").\n",
273 program_name, program_name, ovs_rundir());
274 stream_usage("DATABASE", true, false, true);
275 daemon_usage();
276 vlog_usage();
277 printf("\nDPDK options:\n"
278 "Configuration of DPDK via command-line is removed from this\n"
279 "version of Open vSwitch. DPDK is configured through ovsdb.\n"
280 );
281 printf("\nOther options:\n"
282 " --unixctl=SOCKET override default control socket name\n"
283 " -h, --help display this help message\n"
284 " -V, --version display version information\n");
285 exit(EXIT_SUCCESS);
286 }
287
288 static void
289 ovs_vswitchd_exit(struct unixctl_conn *conn, int argc,
290 const char *argv[], void *exit_args_)
291 {
292 struct ovs_vswitchd_exit_args *exit_args = exit_args_;
293 *exit_args->exiting = true;
294 *exit_args->cleanup = argc == 2 && !strcmp(argv[1], "--cleanup");
295 unixctl_command_reply(conn, NULL);
296 }