Remove "fault" module.
[sliver-openvswitch.git] / vswitchd / ovs-vswitchd.c
1 /* Copyright (c) 2008, 2009 Nicira Networks
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 <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>
25
26 #include "bridge.h"
27 #include "command-line.h"
28 #include "compiler.h"
29 #include "daemon.h"
30 #include "dpif.h"
31 #include "leak-checker.h"
32 #include "netdev.h"
33 #include "ovsdb-idl.h"
34 #include "poll-loop.h"
35 #include "proc-net-compat.h"
36 #include "process.h"
37 #include "signals.h"
38 #include "stream-ssl.h"
39 #include "stream.h"
40 #include "svec.h"
41 #include "timeval.h"
42 #include "unixctl.h"
43 #include "util.h"
44 #include "vconn.h"
45 #include "vswitchd/vswitch-idl.h"
46
47 #include "vlog.h"
48 #define THIS_MODULE VLM_vswitchd
49
50 static const char *parse_options(int argc, char *argv[]);
51 static void usage(void) NO_RETURN;
52
53 int
54 main(int argc, char *argv[])
55 {
56     struct unixctl_server *unixctl;
57     struct signal *sighup;
58     struct ovsdb_idl *idl;
59     const char *remote;
60     bool need_reconfigure;
61     bool inited;
62     unsigned int idl_seqno;
63     int retval;
64
65     set_program_name(argv[0]);
66     time_init();
67     vlog_init();
68     remote = parse_options(argc, argv);
69     signal(SIGPIPE, SIG_IGN);
70     sighup = signal_register(SIGHUP);
71     process_init();
72
73     die_if_already_running();
74     daemonize_start();
75
76     retval = unixctl_server_create(NULL, &unixctl);
77     if (retval) {
78         ovs_fatal(retval, "could not listen for control connections");
79     }
80
81     daemonize_complete();
82
83     idl = ovsdb_idl_create(remote, &ovsrec_idl_class);
84     idl_seqno = ovsdb_idl_get_seqno(idl);
85
86     need_reconfigure = false;
87     inited = false;
88     for (;;) {
89         if (signal_poll(sighup)) {
90             vlog_reopen_log_file();
91         }
92         if (inited && bridge_run()) {
93             need_reconfigure = true;
94         }
95         ovsdb_idl_run(idl);
96         if (idl_seqno != ovsdb_idl_get_seqno(idl)) {
97             idl_seqno = ovsdb_idl_get_seqno(idl);
98             need_reconfigure = true;
99         }
100         if (need_reconfigure) {
101             const struct ovsrec_open_vswitch *cfg;
102
103             need_reconfigure = false;
104             cfg = ovsrec_open_vswitch_first(idl);
105             if (cfg) {
106                 if (inited) {
107                     bridge_reconfigure(cfg);
108                 } else {
109                     bridge_init(cfg);
110                     inited = true;
111                 }
112             }
113         }
114         unixctl_server_run(unixctl);
115         dp_run();
116         netdev_run();
117
118         signal_wait(sighup);
119         if (inited) {
120             bridge_wait();
121         }
122         ovsdb_idl_wait(idl);
123         unixctl_server_wait(unixctl);
124         dp_wait();
125         netdev_wait();
126         poll_block();
127     }
128
129     return 0;
130 }
131
132 static const char *
133 parse_options(int argc, char *argv[])
134 {
135     enum {
136         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
137         OPT_FAKE_PROC_NET,
138         VLOG_OPTION_ENUMS,
139         LEAK_CHECKER_OPTION_ENUMS,
140         OPT_BOOTSTRAP_CA_CERT
141     };
142     static struct option long_options[] = {
143         {"help",        no_argument, 0, 'h'},
144         {"version",     no_argument, 0, 'V'},
145         {"fake-proc-net", no_argument, 0, OPT_FAKE_PROC_NET},
146         DAEMON_LONG_OPTIONS,
147         VLOG_LONG_OPTIONS,
148         LEAK_CHECKER_LONG_OPTIONS,
149 #ifdef HAVE_OPENSSL
150         STREAM_SSL_LONG_OPTIONS
151         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
152         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
153 #endif
154         {0, 0, 0, 0},
155     };
156     char *short_options = long_options_to_short_options(long_options);
157     int error;
158
159     for (;;) {
160         int c;
161
162         c = getopt_long(argc, argv, short_options, long_options, NULL);
163         if (c == -1) {
164             break;
165         }
166
167         switch (c) {
168         case 'H':
169         case 'h':
170             usage();
171
172         case 'V':
173             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
174             exit(EXIT_SUCCESS);
175
176         case OPT_FAKE_PROC_NET:
177             error = proc_net_compat_init();
178             if (error) {
179                 ovs_fatal(error, "failed to initialize /proc/net "
180                           "compatibility");
181             }
182             break;
183
184         VLOG_OPTION_HANDLERS
185         DAEMON_OPTION_HANDLERS
186         LEAK_CHECKER_OPTION_HANDLERS
187
188 #ifdef HAVE_OPENSSL
189         STREAM_SSL_OPTION_HANDLERS
190
191         case OPT_PEER_CA_CERT:
192             stream_ssl_set_peer_ca_cert_file(optarg);
193             break;
194
195         case OPT_BOOTSTRAP_CA_CERT:
196             stream_ssl_set_ca_cert_file(optarg, true);
197             break;
198 #endif
199
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
212     if (argc != 1) {
213         ovs_fatal(0, "database socket is only non-option argument; "
214                 "use --help for usage");
215     }
216
217     return argv[0];
218 }
219
220 static void
221 usage(void)
222 {
223     printf("%s: Open vSwitch daemon\n"
224            "usage: %s [OPTIONS] DATABASE\n"
225            "where DATABASE is a socket on which ovsdb-server is listening.\n",
226            program_name, program_name);
227     stream_usage("DATABASE", true, false, true);
228     daemon_usage();
229     vlog_usage();
230     printf("\nLegacy compatibility options:\n"
231            " --fake-proc-net          simulate some files in /proc/net\n"
232            "\nOther options:\n"
233            "  -h, --help              display this help message\n"
234            "  -V, --version           display version information\n");
235     leak_checker_usage();
236     exit(EXIT_SUCCESS);
237 }