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