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