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