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