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