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