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