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