e11febd4d1a245cc0fb230a9d31a8de7120b25d3
[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 "leak-checker.h"
36 #include "memory.h"
37 #include "netdev.h"
38 #include "openflow/openflow.h"
39 #include "ovsdb-idl.h"
40 #include "poll-loop.h"
41 #include "process.h"
42 #include "signals.h"
43 #include "simap.h"
44 #include "stream-ssl.h"
45 #include "stream.h"
46 #include "stress.h"
47 #include "svec.h"
48 #include "timeval.h"
49 #include "unixctl.h"
50 #include "util.h"
51 #include "vconn.h"
52 #include "vlog.h"
53 #include "lib/vswitch-idl.h"
54 #include "worker.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     if (want_mlockall) {
89 #ifdef HAVE_MLOCKALL
90         if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
91             VLOG_ERR("mlockall failed: %s", strerror(errno));
92         }
93 #else
94         VLOG_ERR("mlockall not supported on this system");
95 #endif
96     }
97
98     worker_start();
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         worker_run();
112         if (signal_poll(sighup)) {
113             vlog_reopen_log_file();
114         }
115         memory_run();
116         if (memory_should_report()) {
117             struct simap usage;
118
119             simap_init(&usage);
120             bridge_get_memory_usage(&usage);
121             memory_report(&usage);
122             simap_destroy(&usage);
123         }
124         bridge_run_fast();
125         bridge_run();
126         bridge_run_fast();
127         unixctl_server_run(unixctl);
128         netdev_run();
129
130         worker_wait();
131         signal_wait(sighup);
132         memory_wait();
133         bridge_wait();
134         unixctl_server_wait(unixctl);
135         netdev_wait();
136         if (exiting) {
137             poll_immediate_wake();
138         }
139         poll_block();
140     }
141     bridge_exit();
142     unixctl_server_destroy(unixctl);
143
144     return 0;
145 }
146
147 static char *
148 parse_options(int argc, char *argv[], char **unixctl_pathp)
149 {
150     enum {
151         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
152         OPT_MLOCKALL,
153         OPT_UNIXCTL,
154         VLOG_OPTION_ENUMS,
155         LEAK_CHECKER_OPTION_ENUMS,
156         OPT_BOOTSTRAP_CA_CERT,
157         OPT_ENABLE_DUMMY,
158         OPT_DISABLE_SYSTEM,
159         DAEMON_OPTION_ENUMS
160     };
161     static const struct option long_options[] = {
162         {"help",        no_argument, NULL, 'h'},
163         {"version",     no_argument, NULL, 'V'},
164         {"mlockall",    no_argument, NULL, OPT_MLOCKALL},
165         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
166         DAEMON_LONG_OPTIONS,
167         VLOG_LONG_OPTIONS,
168         LEAK_CHECKER_LONG_OPTIONS,
169         STREAM_SSL_LONG_OPTIONS,
170         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
171         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
172         {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
173         {"disable-system", no_argument, NULL, OPT_DISABLE_SYSTEM},
174         {NULL, 0, NULL, 0},
175     };
176     char *short_options = long_options_to_short_options(long_options);
177
178     for (;;) {
179         int c;
180
181         c = getopt_long(argc, argv, short_options, long_options, NULL);
182         if (c == -1) {
183             break;
184         }
185
186         switch (c) {
187         case 'h':
188             usage();
189
190         case 'V':
191             ovs_print_version(OFP10_VERSION, OFP10_VERSION);
192             exit(EXIT_SUCCESS);
193
194         case OPT_MLOCKALL:
195             want_mlockall = true;
196             break;
197
198         case OPT_UNIXCTL:
199             *unixctl_pathp = optarg;
200             break;
201
202         VLOG_OPTION_HANDLERS
203         DAEMON_OPTION_HANDLERS
204         LEAK_CHECKER_OPTION_HANDLERS
205         STREAM_SSL_OPTION_HANDLERS
206
207         case OPT_PEER_CA_CERT:
208             stream_ssl_set_peer_ca_cert_file(optarg);
209             break;
210
211         case OPT_BOOTSTRAP_CA_CERT:
212             stream_ssl_set_ca_cert_file(optarg, true);
213             break;
214
215         case OPT_ENABLE_DUMMY:
216             dummy_enable(optarg && !strcmp(optarg, "override"));
217             break;
218
219         case OPT_DISABLE_SYSTEM:
220             dp_blacklist_provider("system");
221             break;
222
223         case '?':
224             exit(EXIT_FAILURE);
225
226         default:
227             abort();
228         }
229     }
230     free(short_options);
231
232     argc -= optind;
233     argv += optind;
234
235     switch (argc) {
236     case 0:
237         return xasprintf("unix:%s/db.sock", ovs_rundir());
238
239     case 1:
240         return xstrdup(argv[0]);
241
242     default:
243         VLOG_FATAL("at most one non-option argument accepted; "
244                    "use --help for usage");
245     }
246 }
247
248 static void
249 usage(void)
250 {
251     printf("%s: Open vSwitch daemon\n"
252            "usage: %s [OPTIONS] [DATABASE]\n"
253            "where DATABASE is a socket on which ovsdb-server is listening\n"
254            "      (default: \"unix:%s/db.sock\").\n",
255            program_name, program_name, ovs_rundir());
256     stream_usage("DATABASE", true, false, true);
257     daemon_usage();
258     vlog_usage();
259     printf("\nOther options:\n"
260            "  --unixctl=SOCKET        override default control socket name\n"
261            "  -h, --help              display this help message\n"
262            "  -V, --version           display version information\n");
263     leak_checker_usage();
264     exit(EXIT_SUCCESS);
265 }
266
267 static void
268 ovs_vswitchd_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
269                   const char *argv[] OVS_UNUSED, void *exiting_)
270 {
271     bool *exiting = exiting_;
272     *exiting = true;
273     unixctl_command_reply(conn, NULL);
274 }