]> git.proxmox.com Git - mirror_ovs.git/blame - vswitchd/ovs-vswitchd.c
ovs-actions: New document describing OVS actions in detail.
[mirror_ovs.git] / vswitchd / ovs-vswitchd.c
CommitLineData
74e98efd 1/* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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"
8a777cf6 35#include "fatal-signal.h"
0d085684 36#include "memory.h"
8b61709d 37#include "netdev.h"
3021ea60 38#include "openflow/openflow.h"
76343538 39#include "ovsdb-idl.h"
9a3cf0ac 40#include "ovs-rcu.h"
898d7b05 41#include "ovs-router.h"
91b8ec6c 42#include "ovs-thread.h"
fd016ae3 43#include "openvswitch/poll-loop.h"
0d085684 44#include "simap.h"
fe55ad15 45#include "stream-ssl.h"
76343538 46#include "stream.h"
064af421
BP
47#include "svec.h"
48#include "timeval.h"
49#include "unixctl.h"
50#include "util.h"
4a1f523f 51#include "openvswitch/vconn.h"
e6211adc 52#include "openvswitch/vlog.h"
eaa67ba8 53#include "lib/vswitch-idl.h"
771680d9 54#include "lib/dns-resolve.h"
064af421 55
d98e6007 56VLOG_DEFINE_THIS_MODULE(vswitchd);
064af421 57
908ff19a
BP
58/* --mlockall: If set, locks all process memory into physical RAM, preventing
59 * the kernel from paging any of its memory to disk. */
60static bool want_mlockall;
61
9e15c889
BP
62static unixctl_cb_func ovs_vswitchd_exit;
63
3542148e 64static char *parse_options(int argc, char *argv[], char **unixctl_path);
cab50449 65OVS_NO_RETURN static void usage(void);
064af421 66
fe13ccdc
AZ
67struct ovs_vswitchd_exit_args {
68 bool *exiting;
69 bool *cleanup;
70};
71
064af421
BP
72int
73main(int argc, char *argv[])
74{
3542148e 75 char *unixctl_path = NULL;
064af421 76 struct unixctl_server *unixctl;
80df177a 77 char *remote;
fe13ccdc
AZ
78 bool exiting, cleanup;
79 struct ovs_vswitchd_exit_args exit_args = {&exiting, &cleanup};
064af421
BP
80 int retval;
81
82 set_program_name(argv[0]);
91b8ec6c 83 ovsthread_id_init();
8a9562d2 84
771680d9 85 dns_resolve_init(true);
5f383751 86 ovs_cmdl_proctitle_init(argc, argv);
42dd41ef 87 service_start(&argc, &argv);
3542148e 88 remote = parse_options(argc, argv, &unixctl_path);
8a777cf6 89 fatal_ignore_sigpipe();
064af421 90
e91b927d 91 daemonize_start(true);
064af421 92
908ff19a
BP
93 if (want_mlockall) {
94#ifdef HAVE_MLOCKALL
95 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
10a89ef0 96 VLOG_ERR("mlockall failed: %s", ovs_strerror(errno));
908ff19a
BP
97 }
98#else
99 VLOG_ERR("mlockall not supported on this system");
100#endif
101 }
102
3542148e 103 retval = unixctl_server_create(unixctl_path, &unixctl);
064af421 104 if (retval) {
4d12270a 105 exit(EXIT_FAILURE);
064af421 106 }
fe13ccdc
AZ
107 unixctl_command_register("exit", "[--cleanup]", 0, 1,
108 ovs_vswitchd_exit, &exit_args);
064af421 109
c5187f17 110 bridge_init(remote);
80df177a
BP
111 free(remote);
112
9e15c889 113 exiting = false;
fe13ccdc 114 cleanup = false;
9e15c889 115 while (!exiting) {
0d085684
BP
116 memory_run();
117 if (memory_should_report()) {
118 struct simap usage;
119
120 simap_init(&usage);
121 bridge_get_memory_usage(&usage);
122 memory_report(&usage);
123 simap_destroy(&usage);
124 }
c5187f17 125 bridge_run();
064af421 126 unixctl_server_run(unixctl);
8b61709d 127 netdev_run();
064af421 128
0d085684 129 memory_wait();
c5187f17 130 bridge_wait();
064af421 131 unixctl_server_wait(unixctl);
8b61709d 132 netdev_wait();
6d37aaf1
BP
133 if (exiting) {
134 poll_immediate_wake();
135 }
064af421 136 poll_block();
42dd41ef
GS
137 if (should_service_stop()) {
138 exiting = true;
139 }
064af421 140 }
fe13ccdc 141 bridge_exit(cleanup);
ee45ad81 142 unixctl_server_destroy(unixctl);
42dd41ef 143 service_stop();
93f55842 144 vlog_disable_async();
9a3cf0ac 145 ovsrcu_exit();
771680d9 146 dns_resolve_destroy();
064af421
BP
147
148 return 0;
149}
150
80df177a 151static char *
3542148e 152parse_options(int argc, char *argv[], char **unixctl_pathp)
064af421
BP
153{
154 enum {
155 OPT_PEER_CA_CERT = UCHAR_MAX + 1,
86a06318 156 OPT_MLOCKALL,
3542148e 157 OPT_UNIXCTL,
064af421 158 VLOG_OPTION_ENUMS,
614c4892 159 OPT_BOOTSTRAP_CA_CERT,
8274ae95 160 OPT_ENABLE_DUMMY,
579a77e0 161 OPT_DISABLE_SYSTEM,
8a9562d2
PS
162 DAEMON_OPTION_ENUMS,
163 OPT_DPDK,
e18a1d08 164 SSL_OPTION_ENUMS,
b4e28b7f 165 OPT_DUMMY_NUMA,
064af421 166 };
07fc4ed3 167 static const struct option long_options[] = {
e3c17733
BP
168 {"help", no_argument, NULL, 'h'},
169 {"version", no_argument, NULL, 'V'},
170 {"mlockall", no_argument, NULL, OPT_MLOCKALL},
3542148e 171 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
064af421
BP
172 DAEMON_LONG_OPTIONS,
173 VLOG_LONG_OPTIONS,
bf8f2167 174 STREAM_SSL_LONG_OPTIONS,
e3c17733
BP
175 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
176 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
0cbfe35d 177 {"enable-dummy", optional_argument, NULL, OPT_ENABLE_DUMMY},
579a77e0 178 {"disable-system", no_argument, NULL, OPT_DISABLE_SYSTEM},
bab69409 179 {"dpdk", optional_argument, NULL, OPT_DPDK},
b4e28b7f 180 {"dummy-numa", required_argument, NULL, OPT_DUMMY_NUMA},
e3c17733 181 {NULL, 0, NULL, 0},
064af421 182 };
5f383751 183 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
064af421
BP
184
185 for (;;) {
186 int c;
187
188 c = getopt_long(argc, argv, short_options, long_options, NULL);
189 if (c == -1) {
190 break;
191 }
192
193 switch (c) {
064af421
BP
194 case 'h':
195 usage();
196
197 case 'V':
11b06842 198 ovs_print_version(0, 0);
40c23a57 199 print_dpdk_version();
064af421
BP
200 exit(EXIT_SUCCESS);
201
86a06318 202 case OPT_MLOCKALL:
908ff19a 203 want_mlockall = true;
86a06318
BP
204 break;
205
3542148e
AL
206 case OPT_UNIXCTL:
207 *unixctl_pathp = optarg;
208 break;
209
064af421
BP
210 VLOG_OPTION_HANDLERS
211 DAEMON_OPTION_HANDLERS
fe55ad15
BP
212 STREAM_SSL_OPTION_HANDLERS
213
064af421 214 case OPT_PEER_CA_CERT:
fe55ad15 215 stream_ssl_set_peer_ca_cert_file(optarg);
064af421 216 break;
6f61c75b
BP
217
218 case OPT_BOOTSTRAP_CA_CERT:
219 stream_ssl_set_ca_cert_file(optarg, true);
220 break;
064af421 221
614c4892 222 case OPT_ENABLE_DUMMY:
8420c7ad 223 dummy_enable(optarg);
614c4892
BP
224 break;
225
579a77e0
EJ
226 case OPT_DISABLE_SYSTEM:
227 dp_blacklist_provider("system");
898d7b05 228 ovs_router_disable_system_routing_table();
579a77e0
EJ
229 break;
230
064af421
BP
231 case '?':
232 exit(EXIT_FAILURE);
233
8a9562d2 234 case OPT_DPDK:
bab69409 235 ovs_fatal(0, "Using --dpdk to configure DPDK is not supported.");
8a9562d2
PS
236 break;
237
b4e28b7f
DDP
238 case OPT_DUMMY_NUMA:
239 ovs_numa_set_dummy(optarg);
240 break;
241
064af421
BP
242 default:
243 abort();
244 }
245 }
246 free(short_options);
247
248 argc -= optind;
249 argv += optind;
250
80df177a
BP
251 switch (argc) {
252 case 0:
253 return xasprintf("unix:%s/db.sock", ovs_rundir());
254
255 case 1:
256 return xstrdup(argv[0]);
257
258 default:
259 VLOG_FATAL("at most one non-option argument accepted; "
279c9e03 260 "use --help for usage");
064af421 261 }
064af421
BP
262}
263
264static void
265usage(void)
266{
f30f26be 267 printf("%s: Open vSwitch daemon\n"
80df177a
BP
268 "usage: %s [OPTIONS] [DATABASE]\n"
269 "where DATABASE is a socket on which ovsdb-server is listening\n"
270 " (default: \"unix:%s/db.sock\").\n",
271 program_name, program_name, ovs_rundir());
9467fe62 272 stream_usage("DATABASE", true, false, true);
064af421
BP
273 daemon_usage();
274 vlog_usage();
d1279464 275 printf("\nDPDK options:\n"
bab69409
AC
276 "Configuration of DPDK via command-line is removed from this\n"
277 "version of Open vSwitch. DPDK is configured through ovsdb.\n"
278 );
0b8b6f71 279 printf("\nOther options:\n"
58397e6c
KT
280 " --unixctl=SOCKET override default control socket name\n"
281 " -h, --help display this help message\n"
282 " -V, --version display version information\n");
064af421
BP
283 exit(EXIT_SUCCESS);
284}
9e15c889
BP
285
286static void
fe13ccdc
AZ
287ovs_vswitchd_exit(struct unixctl_conn *conn, int argc,
288 const char *argv[], void *exit_args_)
9e15c889 289{
fe13ccdc
AZ
290 struct ovs_vswitchd_exit_args *exit_args = exit_args_;
291 *exit_args->exiting = true;
292 *exit_args->cleanup = argc == 2 && !strcmp(argv[1], "--cleanup");
bde9f75d 293 unixctl_command_reply(conn, NULL);
9e15c889 294}