leak-checker: Remove because it cannot be made thread-safe.
[sliver-openvswitch.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 <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 "process.h"
41 #include "signals.h"
42 #include "simap.h"
43 #include "stream-ssl.h"
44 #include "stream.h"
45 #include "stress.h"
46 #include "svec.h"
47 #include "timeval.h"
48 #include "unixctl.h"
49 #include "util.h"
50 #include "vconn.h"
51 #include "vlog.h"
52 #include "lib/vswitch-idl.h"
53 #include "worker.h"
54
55 VLOG_DEFINE_THIS_MODULE(vswitchd);
56
57 /* --mlockall: If set, locks all process memory into physical RAM, preventing
58  * the kernel from paging any of its memory to disk. */
59 static bool want_mlockall;
60
61 static unixctl_cb_func ovs_vswitchd_exit;
62
63 static char *parse_options(int argc, char *argv[], char **unixctl_path);
64 static void usage(void) NO_RETURN;
65
66 int
67 main(int argc, char *argv[])
68 {
69     char *unixctl_path = NULL;
70     struct unixctl_server *unixctl;
71     struct signal *sighup;
72     char *remote;
73     bool exiting;
74     int retval;
75
76     proctitle_init(argc, argv);
77     set_program_name(argv[0]);
78     stress_init_command();
79     remote = parse_options(argc, argv, &unixctl_path);
80     signal(SIGPIPE, SIG_IGN);
81     sighup = signal_register(SIGHUP);
82     process_init();
83     ovsrec_init();
84
85     daemonize_start();
86
87     if (want_mlockall) {
88 #ifdef HAVE_MLOCKALL
89         if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
90             VLOG_ERR("mlockall failed: %s", strerror(errno));
91         }
92 #else
93         VLOG_ERR("mlockall not supported on this system");
94 #endif
95     }
96
97     worker_start();
98
99     retval = unixctl_server_create(unixctl_path, &unixctl);
100     if (retval) {
101         exit(EXIT_FAILURE);
102     }
103     unixctl_command_register("exit", "", 0, 0, ovs_vswitchd_exit, &exiting);
104
105     bridge_init(remote);
106     free(remote);
107
108     exiting = false;
109     while (!exiting) {
110         worker_run();
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         worker_wait();
130         signal_wait(sighup);
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     }
140     bridge_exit();
141     unixctl_server_destroy(unixctl);
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         OPT_BOOTSTRAP_CA_CERT,
155         OPT_ENABLE_DUMMY,
156         OPT_DISABLE_SYSTEM,
157         DAEMON_OPTION_ENUMS
158     };
159     static const struct option long_options[] = {
160         {"help",        no_argument, NULL, 'h'},
161         {"version",     no_argument, NULL, 'V'},
162         {"mlockall",    no_argument, NULL, OPT_MLOCKALL},
163         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
164         DAEMON_LONG_OPTIONS,
165         VLOG_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         STREAM_SSL_OPTION_HANDLERS
202
203         case OPT_PEER_CA_CERT:
204             stream_ssl_set_peer_ca_cert_file(optarg);
205             break;
206
207         case OPT_BOOTSTRAP_CA_CERT:
208             stream_ssl_set_ca_cert_file(optarg, true);
209             break;
210
211         case OPT_ENABLE_DUMMY:
212             dummy_enable(optarg && !strcmp(optarg, "override"));
213             break;
214
215         case OPT_DISABLE_SYSTEM:
216             dp_blacklist_provider("system");
217             break;
218
219         case '?':
220             exit(EXIT_FAILURE);
221
222         default:
223             abort();
224         }
225     }
226     free(short_options);
227
228     argc -= optind;
229     argv += optind;
230
231     switch (argc) {
232     case 0:
233         return xasprintf("unix:%s/db.sock", ovs_rundir());
234
235     case 1:
236         return xstrdup(argv[0]);
237
238     default:
239         VLOG_FATAL("at most one non-option argument accepted; "
240                    "use --help for usage");
241     }
242 }
243
244 static void
245 usage(void)
246 {
247     printf("%s: Open vSwitch daemon\n"
248            "usage: %s [OPTIONS] [DATABASE]\n"
249            "where DATABASE is a socket on which ovsdb-server is listening\n"
250            "      (default: \"unix:%s/db.sock\").\n",
251            program_name, program_name, ovs_rundir());
252     stream_usage("DATABASE", true, false, true);
253     daemon_usage();
254     vlog_usage();
255     printf("\nOther options:\n"
256            "  --unixctl=SOCKET        override default control socket name\n"
257            "  -h, --help              display this help message\n"
258            "  -V, --version           display version information\n");
259     exit(EXIT_SUCCESS);
260 }
261
262 static void
263 ovs_vswitchd_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
264                   const char *argv[] OVS_UNUSED, void *exiting_)
265 {
266     bool *exiting = exiting_;
267     *exiting = true;
268     unixctl_command_reply(conn, NULL);
269 }