xenserver: Store XAPI dbcache as XML in interface-reconfigure.
[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 "dpif.h"
32 #include "fault.h"
33 #include "leak-checker.h"
34 #include "mgmt.h"
35 #include "netdev.h"
36 #include "ovs-vswitchd.h"
37 #include "poll-loop.h"
38 #include "proc-net-compat.h"
39 #include "process.h"
40 #include "signals.h"
41 #include "svec.h"
42 #include "timeval.h"
43 #include "unixctl.h"
44 #include "util.h"
45 #include "vconn-ssl.h"
46 #include "vconn.h"
47
48 #include "vlog.h"
49 #define THIS_MODULE VLM_vswitchd
50
51 static void parse_options(int argc, char *argv[]);
52 static void usage(void) NO_RETURN;
53 static void reload(struct unixctl_conn *, const char *args);
54
55 static bool need_reconfigure;
56 static struct unixctl_conn **conns;
57 static size_t n_conns;
58
59 int
60 main(int argc, char *argv[])
61 {
62     struct unixctl_server *unixctl;
63     struct signal *sighup;
64     int retval;
65
66     set_program_name(argv[0]);
67     register_fault_handlers();
68     time_init();
69     vlog_init();
70     parse_options(argc, argv);
71     signal(SIGPIPE, SIG_IGN);
72     sighup = signal_register(SIGHUP);
73     process_init();
74
75     die_if_already_running();
76     daemonize();
77
78     retval = unixctl_server_create(NULL, &unixctl);
79     if (retval) {
80         ovs_fatal(retval, "could not listen for control connections");
81     }
82     unixctl_command_register("vswitchd/reload", reload);
83
84     cfg_read();
85     mgmt_init();
86     bridge_init();
87     mgmt_reconfigure();
88
89     need_reconfigure = false;
90     for (;;) {
91         if (need_reconfigure || signal_poll(sighup)) {
92             need_reconfigure = false;
93             vlog_reopen_log_file();
94             reconfigure();
95         }
96         if (mgmt_run()) {
97             need_reconfigure = true;
98         }
99         if (bridge_run()) {
100             need_reconfigure = true;
101         }
102         unixctl_server_run(unixctl);
103         dp_run();
104         netdev_run();
105
106         if (need_reconfigure) {
107             poll_immediate_wake();
108         }
109         signal_wait(sighup);
110         mgmt_wait();
111         bridge_wait();
112         unixctl_server_wait(unixctl);
113         dp_wait();
114         netdev_wait();
115         poll_block();
116     }
117
118     return 0;
119 }
120
121 static void
122 reload(struct unixctl_conn *conn, const char *args UNUSED)
123 {
124     need_reconfigure = true;
125     conns = xrealloc(conns, sizeof *conns * (n_conns + 1));
126     conns[n_conns++] = conn;
127 }
128
129 void
130 reconfigure(void)
131 {
132     size_t i;
133
134     cfg_read();
135     bridge_reconfigure();
136     mgmt_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     config_file = argv[0];
226     error = cfg_set_file(config_file);
227     if (error) {
228        ovs_fatal(error, "failed to add configuration file \"%s\"", 
229                 config_file);
230     }
231 }
232
233 static void
234 usage(void)
235 {
236     printf("%s: Open vSwitch daemon\n"
237            "usage: %s [OPTIONS] CONFIG\n"
238            "CONFIG is a configuration file in ovs-vswitchd.conf(5) format.\n",
239            program_name, program_name);
240     daemon_usage();
241     vlog_usage();
242     printf("\nLegacy compatibility options:\n"
243            " --fake-proc-net          simulate some files in /proc/net\n"
244            "\nOther options:\n"
245            "  -h, --help              display this help message\n"
246            "  -V, --version           display version information\n");
247     leak_checker_usage();
248     exit(EXIT_SUCCESS);
249 }