]> git.proxmox.com Git - mirror_ovs.git/blame - vswitchd/ovs-vswitchd.c
worker: Delete library.
[mirror_ovs.git] / vswitchd / ovs-vswitchd.c
CommitLineData
10a89ef0 1/* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
064af421 2 *
a14bc59f
BP
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:
064af421 6 *
a14bc59f 7 * http://www.apache.org/licenses/LICENSE-2.0
064af421 8 *
a14bc59f
BP
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.
064af421
BP
14 */
15
16#include <config.h>
17
064af421
BP
18#include <errno.h>
19#include <getopt.h>
20#include <limits.h>
21#include <signal.h>
22#include <stdlib.h>
23#include <string.h>
86a06318
BP
24#ifdef HAVE_MLOCKALL
25#include <sys/mman.h>
26#endif
064af421
BP
27
28#include "bridge.h"
064af421
BP
29#include "command-line.h"
30#include "compiler.h"
31#include "daemon.h"
80df177a 32#include "dirs.h"
579a77e0 33#include "dpif.h"
614c4892 34#include "dummy.h"
0d085684 35#include "memory.h"
8b61709d 36#include "netdev.h"
3021ea60 37#include "openflow/openflow.h"
76343538 38#include "ovsdb-idl.h"
064af421 39#include "poll-loop.h"
064af421
BP
40#include "process.h"
41#include "signals.h"
0d085684 42#include "simap.h"
fe55ad15 43#include "stream-ssl.h"
76343538 44#include "stream.h"
cc01d0bb 45#include "stress.h"
064af421
BP
46#include "svec.h"
47#include "timeval.h"
48#include "unixctl.h"
49#include "util.h"
064af421 50#include "vconn.h"
5136ce49 51#include "vlog.h"
eaa67ba8 52#include "lib/vswitch-idl.h"
064af421 53
d98e6007 54VLOG_DEFINE_THIS_MODULE(vswitchd);
064af421 55
908ff19a
BP
56/* --mlockall: If set, locks all process memory into physical RAM, preventing
57 * the kernel from paging any of its memory to disk. */
58static bool want_mlockall;
59
9e15c889
BP
60static unixctl_cb_func ovs_vswitchd_exit;
61
3542148e 62static char *parse_options(int argc, char *argv[], char **unixctl_path);
064af421 63static void usage(void) NO_RETURN;
064af421
BP
64
65int
66main(int argc, char *argv[])
67{
3542148e 68 char *unixctl_path = NULL;
064af421
BP
69 struct unixctl_server *unixctl;
70 struct signal *sighup;
80df177a 71 char *remote;
c5187f17 72 bool exiting;
064af421
BP
73 int retval;
74
40f0707c 75 proctitle_init(argc, argv);
064af421 76 set_program_name(argv[0]);
cc01d0bb 77 stress_init_command();
3542148e 78 remote = parse_options(argc, argv, &unixctl_path);
064af421
BP
79 signal(SIGPIPE, SIG_IGN);
80 sighup = signal_register(SIGHUP);
81 process_init();
bd76d25d 82 ovsrec_init();
064af421 83
95440284 84 daemonize_start();
064af421 85
908ff19a
BP
86 if (want_mlockall) {
87#ifdef HAVE_MLOCKALL
88 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
10a89ef0 89 VLOG_ERR("mlockall failed: %s", ovs_strerror(errno));
908ff19a
BP
90 }
91#else
92 VLOG_ERR("mlockall not supported on this system");
93#endif
94 }
95
3542148e 96 retval = unixctl_server_create(unixctl_path, &unixctl);
064af421 97 if (retval) {
4d12270a 98 exit(EXIT_FAILURE);
064af421 99 }
0e15264f 100 unixctl_command_register("exit", "", 0, 0, ovs_vswitchd_exit, &exiting);
064af421 101
c5187f17 102 bridge_init(remote);
80df177a
BP
103 free(remote);
104
9e15c889
BP
105 exiting = false;
106 while (!exiting) {
76343538 107 if (signal_poll(sighup)) {
064af421 108 vlog_reopen_log_file();
064af421 109 }
0d085684
BP
110 memory_run();
111 if (memory_should_report()) {
112 struct simap usage;
113
114 simap_init(&usage);
115 bridge_get_memory_usage(&usage);
116 memory_report(&usage);
117 simap_destroy(&usage);
118 }
5fcc0d00 119 bridge_run_fast();
c5187f17 120 bridge_run();
5fcc0d00 121 bridge_run_fast();
064af421 122 unixctl_server_run(unixctl);
8b61709d 123 netdev_run();
064af421 124
064af421 125 signal_wait(sighup);
0d085684 126 memory_wait();
c5187f17 127 bridge_wait();
064af421 128 unixctl_server_wait(unixctl);
8b61709d 129 netdev_wait();
6d37aaf1
BP
130 if (exiting) {
131 poll_immediate_wake();
132 }
064af421
BP
133 poll_block();
134 }
ee45ad81
BP
135 bridge_exit();
136 unixctl_server_destroy(unixctl);
064af421
BP
137
138 return 0;
139}
140
80df177a 141static char *
3542148e 142parse_options(int argc, char *argv[], char **unixctl_pathp)
064af421
BP
143{
144 enum {
145 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
86a06318 146 OPT_MLOCKALL,
3542148e 147 OPT_UNIXCTL,
064af421 148 VLOG_OPTION_ENUMS,
614c4892 149 OPT_BOOTSTRAP_CA_CERT,
8274ae95 150 OPT_ENABLE_DUMMY,
579a77e0 151 OPT_DISABLE_SYSTEM,
8274ae95 152 DAEMON_OPTION_ENUMS
064af421 153 };
07fc4ed3 154 static const struct option long_options[] = {
e3c17733
BP
155 {"help", no_argument, NULL, 'h'},
156 {"version", no_argument, NULL, 'V'},
157 {"mlockall", no_argument, NULL, OPT_MLOCKALL},
3542148e 158 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
064af421
BP
159 DAEMON_LONG_OPTIONS,
160 VLOG_LONG_OPTIONS,
bf8f2167 161 STREAM_SSL_LONG_OPTIONS,
e3c17733
BP
162 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
163 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
0cbfe35d 164 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
579a77e0 165 {"disable-system", no_argument, NULL, OPT_DISABLE_SYSTEM},
e3c17733 166 {NULL, 0, NULL, 0},
064af421
BP
167 };
168 char *short_options = long_options_to_short_options(long_options);
064af421
BP
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) {
064af421
BP
179 case 'h':
180 usage();
181
182 case 'V':
87ea5e5e 183 ovs_print_version(OFP10_VERSION, OFP10_VERSION);
064af421
BP
184 exit(EXIT_SUCCESS);
185
86a06318 186 case OPT_MLOCKALL:
908ff19a 187 want_mlockall = true;
86a06318
BP
188 break;
189
3542148e
AL
190 case OPT_UNIXCTL:
191 *unixctl_pathp = optarg;
192 break;
193
064af421
BP
194 VLOG_OPTION_HANDLERS
195 DAEMON_OPTION_HANDLERS
fe55ad15
BP
196 STREAM_SSL_OPTION_HANDLERS
197
064af421 198 case OPT_PEER_CA_CERT:
fe55ad15 199 stream_ssl_set_peer_ca_cert_file(optarg);
064af421 200 break;
6f61c75b
BP
201
202 case OPT_BOOTSTRAP_CA_CERT:
203 stream_ssl_set_ca_cert_file(optarg, true);
204 break;
064af421 205
614c4892 206 case OPT_ENABLE_DUMMY:
0cbfe35d 207 dummy_enable(optarg && !strcmp(optarg, "override"));
614c4892
BP
208 break;
209
579a77e0
EJ
210 case OPT_DISABLE_SYSTEM:
211 dp_blacklist_provider("system");
212 break;
213
064af421
BP
214 case '?':
215 exit(EXIT_FAILURE);
216
217 default:
218 abort();
219 }
220 }
221 free(short_options);
222
223 argc -= optind;
224 argv += optind;
225
80df177a
BP
226 switch (argc) {
227 case 0:
228 return xasprintf("unix:%s/db.sock", ovs_rundir());
229
230 case 1:
231 return xstrdup(argv[0]);
232
233 default:
234 VLOG_FATAL("at most one non-option argument accepted; "
279c9e03 235 "use --help for usage");
064af421 236 }
064af421
BP
237}
238
239static void
240usage(void)
241{
f30f26be 242 printf("%s: Open vSwitch daemon\n"
80df177a
BP
243 "usage: %s [OPTIONS] [DATABASE]\n"
244 "where DATABASE is a socket on which ovsdb-server is listening\n"
245 " (default: \"unix:%s/db.sock\").\n",
246 program_name, program_name, ovs_rundir());
9467fe62 247 stream_usage("DATABASE", true, false, true);
064af421
BP
248 daemon_usage();
249 vlog_usage();
0b8b6f71 250 printf("\nOther options:\n"
3542148e 251 " --unixctl=SOCKET override default control socket name\n"
064af421
BP
252 " -h, --help display this help message\n"
253 " -V, --version display version information\n");
064af421
BP
254 exit(EXIT_SUCCESS);
255}
9e15c889
BP
256
257static void
0e15264f
BP
258ovs_vswitchd_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
259 const char *argv[] OVS_UNUSED, void *exiting_)
9e15c889
BP
260{
261 bool *exiting = exiting_;
262 *exiting = true;
bde9f75d 263 unixctl_command_reply(conn, NULL);
9e15c889 264}