xenserver: Add missing argument for fake-iface config
[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 "cfg.h"
28 #include "command-line.h"
29 #include "compiler.h"
30 #include "daemon.h"
31 #include "fault.h"
32 #include "leak-checker.h"
33 #include "mgmt.h"
34 #include "ovs-vswitchd.h"
35 #include "poll-loop.h"
36 #include "port.h"
37 #include "proc-net-compat.h"
38 #include "process.h"
39 #include "signals.h"
40 #include "svec.h"
41 #include "timeval.h"
42 #include "unixctl.h"
43 #include "util.h"
44 #include "vconn-ssl.h"
45 #include "vconn.h"
46
47 #include "vlog.h"
48 #define THIS_MODULE VLM_vswitchd
49
50 static void parse_options(int argc, char *argv[]);
51 static void usage(void) NO_RETURN;
52 static void reload(struct unixctl_conn *, const char *args);
53
54 static bool need_reconfigure;
55 static struct unixctl_conn **conns;
56 static size_t n_conns;
57
58 int
59 main(int argc, char *argv[])
60 {
61     struct unixctl_server *unixctl;
62     struct signal *sighup;
63     int retval;
64
65     set_program_name(argv[0]);
66     register_fault_handlers();
67     time_init();
68     vlog_init();
69     parse_options(argc, argv);
70     signal(SIGPIPE, SIG_IGN);
71     sighup = signal_register(SIGHUP);
72     process_init();
73
74     die_if_already_running();
75     daemonize();
76
77     retval = unixctl_server_create(NULL, &unixctl);
78     if (retval) {
79         ovs_fatal(retval, "could not listen for control connections");
80     }
81     unixctl_command_register("vswitchd/reload", reload);
82
83     retval = cfg_read();
84     if (retval) {
85         ovs_fatal(retval, "could not read config file");
86     }
87     mgmt_init();
88     bridge_init();
89     port_init();
90     mgmt_reconfigure();
91
92     need_reconfigure = false;
93     for (;;) {
94         if (need_reconfigure || signal_poll(sighup)) {
95             need_reconfigure = false;
96             vlog_reopen_log_file();
97             reconfigure();
98         }
99         if (mgmt_run()) {
100             need_reconfigure = true;
101         }
102         if (bridge_run()) {
103             need_reconfigure = true;
104         }
105         unixctl_server_run(unixctl);
106
107         if (need_reconfigure) {
108             poll_immediate_wake();
109         }
110         signal_wait(sighup);
111         mgmt_wait();
112         bridge_wait();
113         unixctl_server_wait(unixctl);
114         poll_block();
115     }
116
117     return 0;
118 }
119
120 static void
121 reload(struct unixctl_conn *conn, const char *args UNUSED)
122 {
123     need_reconfigure = true;
124     conns = xrealloc(conns, sizeof *conns * (n_conns + 1));
125     conns[n_conns++] = conn;
126 }
127
128 void
129 reconfigure(void)
130 {
131     size_t i;
132
133     cfg_read();
134     bridge_reconfigure();
135     mgmt_reconfigure();
136     port_reconfigure();
137
138     for (i = 0; i < n_conns; i++) {
139         unixctl_command_reply(conns[i], 202, NULL);
140     }
141     free(conns);
142     conns = NULL;
143     n_conns = 0;
144 }
145
146 static void
147 parse_options(int argc, char *argv[])
148 {
149     enum {
150         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
151         OPT_FAKE_PROC_NET,
152         VLOG_OPTION_ENUMS,
153         LEAK_CHECKER_OPTION_ENUMS
154     };
155     static struct option long_options[] = {
156         {"help",        no_argument, 0, 'h'},
157         {"version",     no_argument, 0, 'V'},
158         {"fake-proc-net", no_argument, 0, OPT_FAKE_PROC_NET},
159         DAEMON_LONG_OPTIONS,
160         VLOG_LONG_OPTIONS,
161         LEAK_CHECKER_LONG_OPTIONS,
162 #ifdef HAVE_OPENSSL
163         VCONN_SSL_LONG_OPTIONS
164         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
165 #endif
166         {0, 0, 0, 0},
167     };
168     char *short_options = long_options_to_short_options(long_options);
169     const char *config_file;
170     int error;
171
172     for (;;) {
173         int c;
174
175         c = getopt_long(argc, argv, short_options, long_options, NULL);
176         if (c == -1) {
177             break;
178         }
179
180         switch (c) {
181         case 'H':
182         case 'h':
183             usage();
184
185         case 'V':
186             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
187             exit(EXIT_SUCCESS);
188
189         case OPT_FAKE_PROC_NET:
190             error = proc_net_compat_init();
191             if (error) {
192                 ovs_fatal(error, "failed to initialize /proc/net "
193                           "compatibility");
194             }
195             break;
196
197         VLOG_OPTION_HANDLERS
198         DAEMON_OPTION_HANDLERS
199         VCONN_SSL_OPTION_HANDLERS
200         LEAK_CHECKER_OPTION_HANDLERS
201
202 #ifdef HAVE_OPENSSL
203         case OPT_PEER_CA_CERT:
204             vconn_ssl_set_peer_ca_cert_file(optarg);
205             break;
206 #endif
207
208         case '?':
209             exit(EXIT_FAILURE);
210
211         default:
212             abort();
213         }
214     }
215     free(short_options);
216
217     argc -= optind;
218     argv += optind;
219
220     if (argc != 1) {
221         ovs_fatal(0, "config file is only non-option argument; "
222                 "use --help for usage");
223     }
224
225     cfg_init();
226     config_file = argv[0];
227     error = cfg_set_file(config_file);
228     if (error) {
229        ovs_fatal(error, "failed to add configuration file \"%s\"", 
230                 config_file);
231     }
232 }
233
234 static void
235 usage(void)
236 {
237     printf("%s: virtual switch daemon\n"
238            "usage: %s [OPTIONS] CONFIG\n"
239            "CONFIG is a configuration file in ovs-vswitchd.conf(5) format.\n",
240            program_name, program_name);
241     daemon_usage();
242     vlog_usage();
243     printf("\nLegacy compatibility options:\n"
244            " --fake-proc-net          simulate some files in /proc/net\n"
245            "\nOther options:\n"
246            "  -h, --help              display this help message\n"
247            "  -V, --version           display version information\n");
248     leak_checker_usage();
249     exit(EXIT_SUCCESS);
250 }