Merge citrix into master.
[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     retval = cfg_read();
85     if (retval) {
86         ovs_fatal(retval, "could not read config file");
87     }
88     mgmt_init();
89     bridge_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         dp_run();
107         netdev_run();
108
109         if (need_reconfigure) {
110             poll_immediate_wake();
111         }
112         signal_wait(sighup);
113         mgmt_wait();
114         bridge_wait();
115         unixctl_server_wait(unixctl);
116         dp_wait();
117         netdev_wait();
118         poll_block();
119     }
120
121     return 0;
122 }
123
124 static void
125 reload(struct unixctl_conn *conn, const char *args UNUSED)
126 {
127     need_reconfigure = true;
128     conns = xrealloc(conns, sizeof *conns * (n_conns + 1));
129     conns[n_conns++] = conn;
130 }
131
132 void
133 reconfigure(void)
134 {
135     size_t i;
136
137     cfg_read();
138     bridge_reconfigure();
139     mgmt_reconfigure();
140
141     for (i = 0; i < n_conns; i++) {
142         unixctl_command_reply(conns[i], 202, NULL);
143     }
144     free(conns);
145     conns = NULL;
146     n_conns = 0;
147 }
148
149 static void
150 parse_options(int argc, char *argv[])
151 {
152     enum {
153         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
154         OPT_FAKE_PROC_NET,
155         VLOG_OPTION_ENUMS,
156         LEAK_CHECKER_OPTION_ENUMS
157     };
158     static struct option long_options[] = {
159         {"help",        no_argument, 0, 'h'},
160         {"version",     no_argument, 0, 'V'},
161         {"fake-proc-net", no_argument, 0, OPT_FAKE_PROC_NET},
162         DAEMON_LONG_OPTIONS,
163         VLOG_LONG_OPTIONS,
164         LEAK_CHECKER_LONG_OPTIONS,
165 #ifdef HAVE_OPENSSL
166         VCONN_SSL_LONG_OPTIONS
167         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
168 #endif
169         {0, 0, 0, 0},
170     };
171     char *short_options = long_options_to_short_options(long_options);
172     const char *config_file;
173     int error;
174
175     for (;;) {
176         int c;
177
178         c = getopt_long(argc, argv, short_options, long_options, NULL);
179         if (c == -1) {
180             break;
181         }
182
183         switch (c) {
184         case 'H':
185         case 'h':
186             usage();
187
188         case 'V':
189             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
190             exit(EXIT_SUCCESS);
191
192         case OPT_FAKE_PROC_NET:
193             error = proc_net_compat_init();
194             if (error) {
195                 ovs_fatal(error, "failed to initialize /proc/net "
196                           "compatibility");
197             }
198             break;
199
200         VLOG_OPTION_HANDLERS
201         DAEMON_OPTION_HANDLERS
202         VCONN_SSL_OPTION_HANDLERS
203         LEAK_CHECKER_OPTION_HANDLERS
204
205 #ifdef HAVE_OPENSSL
206         case OPT_PEER_CA_CERT:
207             vconn_ssl_set_peer_ca_cert_file(optarg);
208             break;
209 #endif
210
211         case '?':
212             exit(EXIT_FAILURE);
213
214         default:
215             abort();
216         }
217     }
218     free(short_options);
219
220     argc -= optind;
221     argv += optind;
222
223     if (argc != 1) {
224         ovs_fatal(0, "config file is only non-option argument; "
225                 "use --help for usage");
226     }
227
228     cfg_init();
229     config_file = argv[0];
230     error = cfg_set_file(config_file);
231     if (error) {
232        ovs_fatal(error, "failed to add configuration file \"%s\"", 
233                 config_file);
234     }
235 }
236
237 static void
238 usage(void)
239 {
240     printf("%s: Open vSwitch daemon\n"
241            "usage: %s [OPTIONS] CONFIG\n"
242            "CONFIG is a configuration file in ovs-vswitchd.conf(5) format.\n",
243            program_name, program_name);
244     daemon_usage();
245     vlog_usage();
246     printf("\nLegacy compatibility options:\n"
247            " --fake-proc-net          simulate some files in /proc/net\n"
248            "\nOther options:\n"
249            "  -h, --help              display this help message\n"
250            "  -V, --version           display version information\n");
251     leak_checker_usage();
252     exit(EXIT_SUCCESS);
253 }