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