Don't unnecessarily link against -lresolv and -ldl.
[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 <stdlib.h>
40 #include <string.h>
41
42 #include "buffer.h"
43 #include "command-line.h"
44 #include "compiler.h"
45 #include "daemon.h"
46 #include "fault.h"
47 #include "learning-switch.h"
48 #include "openflow.h"
49 #include "poll-loop.h"
50 #include "rconn.h"
51 #include "util.h"
52 #include "vconn-ssl.h"
53 #include "vconn.h"
54 #include "vlog-socket.h"
55
56 #include "vlog.h"
57 #define THIS_MODULE VLM_controller
58
59 #define MAX_SWITCHES 16
60 #define MAX_LISTENERS 16
61
62 struct switch_ {
63     struct lswitch *lswitch;
64     struct rconn *rconn;
65 };
66
67 /* Learn the ports on which MAC addresses appear? */
68 static bool learn_macs = true;
69
70 /* Set up flows?  (If not, every packet is processed at the controller.) */
71 static bool setup_flows = true;
72
73 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
74 static int max_idle = 60;
75
76 static int do_switching(struct switch_ *);
77 static void new_switch(struct switch_ *, struct vconn *, const char *name);
78 static void parse_options(int argc, char *argv[]);
79 static void usage(void) NO_RETURN;
80
81 int
82 main(int argc, char *argv[])
83 {
84     struct switch_ switches[MAX_SWITCHES];
85     struct vconn *listeners[MAX_LISTENERS];
86     int n_switches, n_listeners;
87     int retval;
88     int i;
89
90     set_program_name(argv[0]);
91     register_fault_handlers();
92     vlog_init();
93     parse_options(argc, argv);
94
95     if (argc - optind < 1) {
96         fatal(0, "at least one vconn argument required; use --help for usage");
97     }
98
99     retval = vlog_server_listen(NULL, NULL);
100     if (retval) {
101         fatal(retval, "Could not listen for vlog connections");
102     }
103
104     n_switches = n_listeners = 0;
105     for (i = optind; i < argc; i++) {
106         const char *name = argv[i];
107         struct vconn *vconn;
108         int retval;
109
110         retval = vconn_open(name, &vconn);
111         if (retval) {
112             VLOG_ERR("%s: connect: %s", name, strerror(retval));
113             continue;
114         }
115
116         if (vconn_is_passive(vconn)) {
117             if (n_listeners >= MAX_LISTENERS) {
118                 fatal(0, "max %d passive connections", n_listeners);
119             }
120             listeners[n_listeners++] = vconn;
121         } else {
122             if (n_switches >= MAX_SWITCHES) {
123                 fatal(0, "max %d switch connections", n_switches);
124             }
125             new_switch(&switches[n_switches++], vconn, name);
126         }
127     }
128     if (n_switches == 0 && n_listeners == 0) {
129         fatal(0, "no active or passive switch connections");
130     }
131
132     daemonize();
133
134     while (n_switches > 0 || n_listeners > 0) {
135         int iteration;
136         int i;
137
138         /* Accept connections on listening vconns. */
139         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
140             struct vconn *new_vconn;
141             int retval;
142
143             retval = vconn_accept(listeners[i], &new_vconn);
144             if (!retval || retval == EAGAIN) {
145                 if (!retval) {
146                     new_switch(&switches[n_switches++], new_vconn, "tcp");
147                 }
148                 i++;
149             } else {
150                 vconn_close(listeners[i]);
151                 listeners[i] = listeners[--n_listeners];
152             }
153         }
154
155         /* Do some switching work.  Limit the number of iterations so that
156          * callbacks registered with the poll loop don't starve. */
157         for (iteration = 0; iteration < 50; iteration++) {
158             bool progress = false;
159             for (i = 0; i < n_switches; ) {
160                 struct switch_ *this = &switches[i];
161                 int retval = do_switching(this);
162                 if (!retval || retval == EAGAIN) {
163                     if (!retval) {
164                         progress = true;
165                     }
166                     i++;
167                 } else {
168                     lswitch_destroy(this->lswitch);
169                     rconn_destroy(this->rconn);
170                     switches[i] = switches[--n_switches];
171                 }
172             }
173             if (!progress) {
174                 break;
175             }
176         }
177
178         /* Wait for something to happen. */
179         if (n_switches < MAX_SWITCHES) {
180             for (i = 0; i < n_listeners; i++) {
181                 vconn_accept_wait(listeners[i]);
182             }
183         }
184         for (i = 0; i < n_switches; i++) {
185             struct switch_ *sw = &switches[i];
186             rconn_run_wait(sw->rconn);
187             rconn_recv_wait(sw->rconn);
188         }
189         poll_block();
190     }
191
192     return 0;
193 }
194
195 static void
196 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
197 {
198     sw->rconn = rconn_new_from_vconn(name, 128, vconn);
199     sw->lswitch = lswitch_create(sw->rconn, learn_macs,
200                                  setup_flows ? max_idle : -1);
201 }
202
203 static int
204 do_switching(struct switch_ *sw)
205 {
206     unsigned int packets_sent;
207     struct buffer *msg;
208
209     packets_sent = rconn_packets_sent(sw->rconn);
210
211     msg = rconn_recv(sw->rconn);
212     if (msg) {
213         lswitch_process_packet(sw->lswitch, sw->rconn, msg);
214         buffer_delete(msg);
215     }
216     rconn_run(sw->rconn);
217
218     return (!rconn_is_alive(sw->rconn) ? EOF
219             : rconn_packets_sent(sw->rconn) != packets_sent ? 0
220             : EAGAIN);
221 }
222
223 static void
224 parse_options(int argc, char *argv[])
225 {
226     enum { OPT_MAX_IDLE = UCHAR_MAX + 1 };
227     static struct option long_options[] = {
228         {"detach",      no_argument, 0, 'D'},
229         {"pidfile",     optional_argument, 0, 'P'},
230         {"hub",         no_argument, 0, 'H'},
231         {"noflow",      no_argument, 0, 'n'},
232         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
233         {"verbose",     optional_argument, 0, 'v'},
234         {"help",        no_argument, 0, 'h'},
235         {"version",     no_argument, 0, 'V'},
236         VCONN_SSL_LONG_OPTIONS
237         {0, 0, 0, 0},
238     };
239     char *short_options = long_options_to_short_options(long_options);
240
241     for (;;) {
242         int indexptr;
243         int c;
244
245         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
246         if (c == -1) {
247             break;
248         }
249
250         switch (c) {
251         case 'D':
252             set_detach();
253             break;
254
255         case 'P':
256             set_pidfile(optarg ? optarg : "controller.pid");
257             break;
258
259         case 'H':
260             learn_macs = false;
261             break;
262
263         case 'n':
264             setup_flows = false;
265             break;
266
267         case OPT_MAX_IDLE:
268             if (!strcmp(optarg, "permanent")) {
269                 max_idle = OFP_FLOW_PERMANENT;
270             } else {
271                 max_idle = atoi(optarg);
272                 if (max_idle < 1 || max_idle > 65535) {
273                     fatal(0, "--max-idle argument must be between 1 and "
274                           "65535 or the word 'permanent'");
275                 }
276             }
277             break;
278
279         case 'h':
280             usage();
281
282         case 'V':
283             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
284             exit(EXIT_SUCCESS);
285
286         case 'v':
287             vlog_set_verbosity(optarg);
288             break;
289
290         VCONN_SSL_OPTION_HANDLERS
291
292         case '?':
293             exit(EXIT_FAILURE);
294
295         default:
296             abort();
297         }
298     }
299     free(short_options);
300 }
301
302 static void
303 usage(void)
304 {
305     printf("%s: OpenFlow controller\n"
306            "usage: %s [OPTIONS] METHOD\n"
307            "where METHOD is any OpenFlow connection method.\n",
308            program_name, program_name);
309     vconn_usage(true, true);
310     printf("\nOther options:\n"
311            "  -D, --detach            run in background as daemon\n"
312            "  -P, --pidfile[=FILE]    create pidfile (default: %s/controller.pid)\n"
313            "  -H, --hub               act as hub instead of learning switch\n"
314            "  -n, --noflow            pass traffic, but don't add flows\n"
315            "  --max-idle=SECS         max idle time for new flows\n"
316            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
317            "  -v, --verbose           set maximum verbosity level\n"
318            "  -h, --help              display this help message\n"
319            "  -V, --version           display version information\n",
320            RUNDIR);
321     exit(EXIT_SUCCESS);
322 }