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