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