Make it easier to bootstrap the PKI for SSL connections in OpenFlow.
[sliver-openvswitch.git] / controller / controller.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <getopt.h>
38 #include <limits.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "command-line.h"
44 #include "compiler.h"
45 #include "daemon.h"
46 #include "fault.h"
47 #include "learning-switch.h"
48 #include "ofpbuf.h"
49 #include "openflow.h"
50 #include "poll-loop.h"
51 #include "rconn.h"
52 #include "timeval.h"
53 #include "util.h"
54 #include "vconn-ssl.h"
55 #include "vconn.h"
56 #include "vlog-socket.h"
57
58 #include "vlog.h"
59 #define THIS_MODULE VLM_controller
60
61 #define MAX_SWITCHES 16
62 #define MAX_LISTENERS 16
63
64 struct switch_ {
65     struct lswitch *lswitch;
66     struct rconn *rconn;
67 };
68
69 /* Learn the ports on which MAC addresses appear? */
70 static bool learn_macs = true;
71
72 /* Set up flows?  (If not, every packet is processed at the controller.) */
73 static bool setup_flows = true;
74
75 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
76 static int max_idle = 60;
77
78 static int do_switching(struct switch_ *);
79 static void new_switch(struct switch_ *, struct vconn *, const char *name);
80 static void parse_options(int argc, char *argv[]);
81 static void usage(void) NO_RETURN;
82
83 int
84 main(int argc, char *argv[])
85 {
86     struct switch_ switches[MAX_SWITCHES];
87     struct pvconn *listeners[MAX_LISTENERS];
88     int n_switches, n_listeners;
89     int retval;
90     int i;
91
92     set_program_name(argv[0]);
93     register_fault_handlers();
94     time_init();
95     vlog_init();
96     parse_options(argc, argv);
97     signal(SIGPIPE, SIG_IGN);
98
99     if (argc - optind < 1) {
100         ofp_fatal(0, "at least one vconn argument required; "
101                   "use --help for usage");
102     }
103
104     retval = vlog_server_listen(NULL, NULL);
105     if (retval) {
106         ofp_fatal(retval, "Could not listen for vlog connections");
107     }
108
109     n_switches = n_listeners = 0;
110     for (i = optind; i < argc; i++) {
111         const char *name = argv[i];
112         struct vconn *vconn;
113         int retval;
114
115         retval = vconn_open(name, OFP_VERSION, &vconn);
116         if (!retval) {
117             if (n_switches >= MAX_SWITCHES) {
118                 ofp_fatal(0, "max %d switch connections", n_switches);
119             }
120             new_switch(&switches[n_switches++], vconn, name);
121             continue;
122         } else if (retval == EAFNOSUPPORT) {
123             struct pvconn *pvconn;
124             retval = pvconn_open(name, &pvconn);
125             if (!retval) {
126                 if (n_listeners >= MAX_LISTENERS) {
127                     ofp_fatal(0, "max %d passive connections", n_listeners);
128                 }
129                 listeners[n_listeners++] = pvconn;
130             }
131         }
132         if (retval) {
133             VLOG_ERR("%s: connect: %s", name, strerror(retval));
134         }
135     }
136     if (n_switches == 0 && n_listeners == 0) {
137         ofp_fatal(0, "no active or passive switch connections");
138     }
139
140     die_if_already_running();
141     daemonize();
142
143     while (n_switches > 0 || n_listeners > 0) {
144         int iteration;
145         int i;
146
147         /* Accept connections on listening vconns. */
148         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
149             struct vconn *new_vconn;
150             int retval;
151
152             retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
153             if (!retval || retval == EAGAIN) {
154                 if (!retval) {
155                     new_switch(&switches[n_switches++], new_vconn, "tcp");
156                 }
157                 i++;
158             } else {
159                 pvconn_close(listeners[i]);
160                 listeners[i] = listeners[--n_listeners];
161             }
162         }
163
164         /* Do some switching work.  Limit the number of iterations so that
165          * callbacks registered with the poll loop don't starve. */
166         for (iteration = 0; iteration < 50; iteration++) {
167             bool progress = false;
168             for (i = 0; i < n_switches; ) {
169                 struct switch_ *this = &switches[i];
170                 int retval = do_switching(this);
171                 if (!retval || retval == EAGAIN) {
172                     if (!retval) {
173                         progress = true;
174                     }
175                     i++;
176                 } else {
177                     lswitch_destroy(this->lswitch);
178                     rconn_destroy(this->rconn);
179                     switches[i] = switches[--n_switches];
180                 }
181             }
182             if (!progress) {
183                 break;
184             }
185         }
186
187         /* Wait for something to happen. */
188         if (n_switches < MAX_SWITCHES) {
189             for (i = 0; i < n_listeners; i++) {
190                 pvconn_wait(listeners[i]);
191             }
192         }
193         for (i = 0; i < n_switches; i++) {
194             struct switch_ *sw = &switches[i];
195             rconn_run_wait(sw->rconn);
196             rconn_recv_wait(sw->rconn);
197         }
198         poll_block();
199     }
200
201     return 0;
202 }
203
204 static void
205 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
206 {
207     sw->rconn = rconn_new_from_vconn(name, vconn);
208     sw->lswitch = lswitch_create(sw->rconn, learn_macs,
209                                  setup_flows ? max_idle : -1);
210 }
211
212 static int
213 do_switching(struct switch_ *sw)
214 {
215     unsigned int packets_sent;
216     struct ofpbuf *msg;
217
218     packets_sent = rconn_packets_sent(sw->rconn);
219
220     msg = rconn_recv(sw->rconn);
221     if (msg) {
222         lswitch_process_packet(sw->lswitch, sw->rconn, msg);
223         ofpbuf_delete(msg);
224     }
225     rconn_run(sw->rconn);
226
227     return (!rconn_is_alive(sw->rconn) ? EOF
228             : rconn_packets_sent(sw->rconn) != packets_sent ? 0
229             : EAGAIN);
230 }
231
232 static void
233 parse_options(int argc, char *argv[])
234 {
235     enum {
236         OPT_MAX_IDLE = UCHAR_MAX + 1,
237         OPT_PEER_CA_CERT
238     };
239     static struct option long_options[] = {
240         {"detach",      no_argument, 0, 'D'},
241         {"pidfile",     optional_argument, 0, 'P'},
242         {"force",       no_argument, 0, 'f'},
243         {"hub",         no_argument, 0, 'H'},
244         {"noflow",      no_argument, 0, 'n'},
245         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
246         {"verbose",     optional_argument, 0, 'v'},
247         {"help",        no_argument, 0, 'h'},
248         {"version",     no_argument, 0, 'V'},
249 #ifdef HAVE_OPENSSL
250         VCONN_SSL_LONG_OPTIONS
251         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
252 #endif
253         {0, 0, 0, 0},
254     };
255     char *short_options = long_options_to_short_options(long_options);
256
257     for (;;) {
258         int indexptr;
259         int c;
260
261         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
262         if (c == -1) {
263             break;
264         }
265
266         switch (c) {
267         case 'D':
268             set_detach();
269             break;
270
271         case 'P':
272             set_pidfile(optarg);
273             break;
274
275         case 'f':
276             ignore_existing_pidfile();
277             break;
278
279         case 'H':
280             learn_macs = false;
281             break;
282
283         case 'n':
284             setup_flows = false;
285             break;
286
287         case OPT_MAX_IDLE:
288             if (!strcmp(optarg, "permanent")) {
289                 max_idle = OFP_FLOW_PERMANENT;
290             } else {
291                 max_idle = atoi(optarg);
292                 if (max_idle < 1 || max_idle > 65535) {
293                     ofp_fatal(0, "--max-idle argument must be between 1 and "
294                               "65535 or the word 'permanent'");
295                 }
296             }
297             break;
298
299         case 'h':
300             usage();
301
302         case 'V':
303             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
304             exit(EXIT_SUCCESS);
305
306         case 'v':
307             vlog_set_verbosity(optarg);
308             break;
309
310 #ifdef HAVE_OPENSSL
311         VCONN_SSL_OPTION_HANDLERS
312
313         case OPT_PEER_CA_CERT:
314             vconn_ssl_set_peer_ca_cert_file(optarg);
315             break;
316 #endif
317
318         case '?':
319             exit(EXIT_FAILURE);
320
321         default:
322             abort();
323         }
324     }
325     free(short_options);
326 }
327
328 static void
329 usage(void)
330 {
331     printf("%s: OpenFlow controller\n"
332            "usage: %s [OPTIONS] METHOD\n"
333            "where METHOD is any OpenFlow connection method.\n",
334            program_name, program_name);
335     vconn_usage(true, true, false);
336     printf("\nOther options:\n"
337            "  -D, --detach            run in background as daemon\n"
338            "  -P, --pidfile[=FILE]    create pidfile (default: %s/controller.pid)\n"
339            "  -f, --force             with -P, start even if already running\n"
340            "  -H, --hub               act as hub instead of learning switch\n"
341            "  -n, --noflow            pass traffic, but don't add flows\n"
342            "  --max-idle=SECS         max idle time for new flows\n"
343            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
344            "  -v, --verbose           set maximum verbosity level\n"
345            "  -h, --help              display this help message\n"
346            "  -V, --version           display version information\n",
347            RUNDIR);
348     exit(EXIT_SUCCESS);
349 }