]> git.proxmox.com Git - mirror_ovs.git/blob - vswitchd/ovs-vswitchd.c
DNS: Add basic support for asynchronous DNS resolving
[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 }
98 #else
99 VLOG_ERR("mlockall not supported on this system");
100 #endif
101 }
102
103 retval = unixctl_server_create(unixctl_path, &unixctl);
104 if (retval) {
105 exit(EXIT_FAILURE);
106 }
107 unixctl_command_register("exit", "[--cleanup]", 0, 1,
108 ovs_vswitchd_exit, &exit_args);
109
110 bridge_init(remote);
111 free(remote);
112
113 exiting = false;
114 cleanup = false;
115 while (!exiting) {
116 memory_run();
117 if (memory_should_report()) {
118 struct simap usage;
119
120 simap_init(&usage);
121 bridge_get_memory_usage(&usage);
122 memory_report(&usage);
123 simap_destroy(&usage);
124 }
125 bridge_run();
126 unixctl_server_run(unixctl);
127 netdev_run();
128
129 memory_wait();
130 bridge_wait();
131 unixctl_server_wait(unixctl);
132 netdev_wait();
133 if (exiting) {
134 poll_immediate_wake();
135 }
136 poll_block();
137 if (should_service_stop()) {
138 exiting = true;
139 }
140 }
141 bridge_exit(cleanup);
142 unixctl_server_destroy(unixctl);
143 service_stop();
144 vlog_disable_async();
145 ovsrcu_exit();
146 dns_resolve_destroy();
147
148 return 0;
149 }
150
151 static char *
152 parse_options(int argc, char *argv[], char **unixctl_pathp)
153 {
154 enum {
155 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
156 OPT_MLOCKALL,
157 OPT_UNIXCTL,
158 VLOG_OPTION_ENUMS,
159 OPT_BOOTSTRAP_CA_CERT,
160 OPT_ENABLE_DUMMY,
161 OPT_DISABLE_SYSTEM,
162 DAEMON_OPTION_ENUMS,
163 OPT_DPDK,
164 SSL_OPTION_ENUMS,
165 OPT_DUMMY_NUMA,
166 };
167 static const struct option long_options[] = {
168 {"help", no_argument, NULL, 'h'},
169 {"version", no_argument, NULL, 'V'},
170 {"mlockall", no_argument, NULL, OPT_MLOCKALL},
171 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
172 DAEMON_LONG_OPTIONS,
173 VLOG_LONG_OPTIONS,
174 STREAM_SSL_LONG_OPTIONS,
175 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
176 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
177 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
178 {"disable-system", no_argument, NULL, OPT_DISABLE_SYSTEM},
179 {"dpdk", optional_argument, NULL, OPT_DPDK},
180 {"dummy-numa", required_argument, NULL, OPT_DUMMY_NUMA},
181 {NULL, 0, NULL, 0},
182 };
183 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
184
185 for (;;) {
186 int c;
187
188 c = getopt_long(argc, argv, short_options, long_options, NULL);
189 if (c == -1) {
190 break;
191 }
192
193 switch (c) {
194 case 'h':
195 usage();
196
197 case 'V':
198 ovs_print_version(0, 0);
199 print_dpdk_version();
200 exit(EXIT_SUCCESS);
201
202 case OPT_MLOCKALL:
203 want_mlockall = true;
204 break;
205
206 case OPT_UNIXCTL:
207 *unixctl_pathp = optarg;
208 break;
209
210 VLOG_OPTION_HANDLERS
211 DAEMON_OPTION_HANDLERS
212 STREAM_SSL_OPTION_HANDLERS
213
214 case OPT_PEER_CA_CERT:
215 stream_ssl_set_peer_ca_cert_file(optarg);
216 break;
217
218 case OPT_BOOTSTRAP_CA_CERT:
219 stream_ssl_set_ca_cert_file(optarg, true);
220 break;
221
222 case OPT_ENABLE_DUMMY:
223 dummy_enable(optarg);
224 break;
225
226 case OPT_DISABLE_SYSTEM:
227 dp_blacklist_provider("system");
228 ovs_router_disable_system_routing_table();
229 break;
230
231 case '?':
232 exit(EXIT_FAILURE);
233
234 case OPT_DPDK:
235 ovs_fatal(0, "Using --dpdk to configure DPDK is not supported.");
236 break;
237
238 case OPT_DUMMY_NUMA:
239 ovs_numa_set_dummy(optarg);
240 break;
241
242 default:
243 abort();
244 }
245 }
246 free(short_options);
247
248 argc -= optind;
249 argv += optind;
250
251 switch (argc) {
252 case 0:
253 return xasprintf("unix:%s/db.sock", ovs_rundir());
254
255 case 1:
256 return xstrdup(argv[0]);
257
258 default:
259 VLOG_FATAL("at most one non-option argument accepted; "
260 "use --help for usage");
261 }
262 }
263
264 static void
265 usage(void)
266 {
267 printf("%s: Open vSwitch daemon\n"
268 "usage: %s [OPTIONS] [DATABASE]\n"
269 "where DATABASE is a socket on which ovsdb-server is listening\n"
270 " (default: \"unix:%s/db.sock\").\n",
271 program_name, program_name, ovs_rundir());
272 stream_usage("DATABASE", true, false, true);
273 daemon_usage();
274 vlog_usage();
275 printf("\nDPDK options:\n"
276 "Configuration of DPDK via command-line is removed from this\n"
277 "version of Open vSwitch. DPDK is configured through ovsdb.\n"
278 );
279 printf("\nOther options:\n"
280 " --unixctl=SOCKET override default control socket name\n"
281 " -h, --help display this help message\n"
282 " -V, --version display version information\n");
283 exit(EXIT_SUCCESS);
284 }
285
286 static void
287 ovs_vswitchd_exit(struct unixctl_conn *conn, int argc,
288 const char *argv[], void *exit_args_)
289 {
290 struct ovs_vswitchd_exit_args *exit_args = exit_args_;
291 *exit_args->exiting = true;
292 *exit_args->cleanup = argc == 2 && !strcmp(argv[1], "--cleanup");
293 unixctl_command_reply(conn, NULL);
294 }