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