Rename utility functions to avoid partner namespace conflicts.
[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 vconn *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, &vconn);
116         if (retval) {
117             VLOG_ERR("%s: connect: %s", name, strerror(retval));
118             continue;
119         }
120
121         if (vconn_is_passive(vconn)) {
122             if (n_listeners >= MAX_LISTENERS) {
123                 ofp_fatal(0, "max %d passive connections", n_listeners);
124             }
125             listeners[n_listeners++] = vconn;
126         } else {
127             if (n_switches >= MAX_SWITCHES) {
128                 ofp_fatal(0, "max %d switch connections", n_switches);
129             }
130             new_switch(&switches[n_switches++], vconn, name);
131         }
132     }
133     if (n_switches == 0 && n_listeners == 0) {
134         ofp_fatal(0, "no active or passive switch connections");
135     }
136
137     die_if_already_running();
138     daemonize();
139
140     while (n_switches > 0 || n_listeners > 0) {
141         int iteration;
142         int i;
143
144         /* Accept connections on listening vconns. */
145         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
146             struct vconn *new_vconn;
147             int retval;
148
149             retval = vconn_accept(listeners[i], &new_vconn);
150             if (!retval || retval == EAGAIN) {
151                 if (!retval) {
152                     new_switch(&switches[n_switches++], new_vconn, "tcp");
153                 }
154                 i++;
155             } else {
156                 vconn_close(listeners[i]);
157                 listeners[i] = listeners[--n_listeners];
158             }
159         }
160
161         /* Do some switching work.  Limit the number of iterations so that
162          * callbacks registered with the poll loop don't starve. */
163         for (iteration = 0; iteration < 50; iteration++) {
164             bool progress = false;
165             for (i = 0; i < n_switches; ) {
166                 struct switch_ *this = &switches[i];
167                 int retval = do_switching(this);
168                 if (!retval || retval == EAGAIN) {
169                     if (!retval) {
170                         progress = true;
171                     }
172                     i++;
173                 } else {
174                     lswitch_destroy(this->lswitch);
175                     rconn_destroy(this->rconn);
176                     switches[i] = switches[--n_switches];
177                 }
178             }
179             if (!progress) {
180                 break;
181             }
182         }
183
184         /* Wait for something to happen. */
185         if (n_switches < MAX_SWITCHES) {
186             for (i = 0; i < n_listeners; i++) {
187                 vconn_accept_wait(listeners[i]);
188             }
189         }
190         for (i = 0; i < n_switches; i++) {
191             struct switch_ *sw = &switches[i];
192             rconn_run_wait(sw->rconn);
193             rconn_recv_wait(sw->rconn);
194         }
195         poll_block();
196     }
197
198     return 0;
199 }
200
201 static void
202 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
203 {
204     sw->rconn = rconn_new_from_vconn(name, vconn);
205     sw->lswitch = lswitch_create(sw->rconn, learn_macs,
206                                  setup_flows ? max_idle : -1);
207 }
208
209 static int
210 do_switching(struct switch_ *sw)
211 {
212     unsigned int packets_sent;
213     struct ofpbuf *msg;
214
215     packets_sent = rconn_packets_sent(sw->rconn);
216
217     msg = rconn_recv(sw->rconn);
218     if (msg) {
219         lswitch_process_packet(sw->lswitch, sw->rconn, msg);
220         ofpbuf_delete(msg);
221     }
222     rconn_run(sw->rconn);
223
224     return (!rconn_is_alive(sw->rconn) ? EOF
225             : rconn_packets_sent(sw->rconn) != packets_sent ? 0
226             : EAGAIN);
227 }
228
229 static void
230 parse_options(int argc, char *argv[])
231 {
232     enum { OPT_MAX_IDLE = UCHAR_MAX + 1 };
233     static struct option long_options[] = {
234         {"detach",      no_argument, 0, 'D'},
235         {"pidfile",     optional_argument, 0, 'P'},
236         {"force",       no_argument, 0, 'f'},
237         {"hub",         no_argument, 0, 'H'},
238         {"noflow",      no_argument, 0, 'n'},
239         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
240         {"verbose",     optional_argument, 0, 'v'},
241         {"help",        no_argument, 0, 'h'},
242         {"version",     no_argument, 0, 'V'},
243         VCONN_SSL_LONG_OPTIONS
244         {0, 0, 0, 0},
245     };
246     char *short_options = long_options_to_short_options(long_options);
247
248     for (;;) {
249         int indexptr;
250         int c;
251
252         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
253         if (c == -1) {
254             break;
255         }
256
257         switch (c) {
258         case 'D':
259             set_detach();
260             break;
261
262         case 'P':
263             set_pidfile(optarg);
264             break;
265
266         case 'f':
267             ignore_existing_pidfile();
268             break;
269
270         case 'H':
271             learn_macs = false;
272             break;
273
274         case 'n':
275             setup_flows = false;
276             break;
277
278         case OPT_MAX_IDLE:
279             if (!strcmp(optarg, "permanent")) {
280                 max_idle = OFP_FLOW_PERMANENT;
281             } else {
282                 max_idle = atoi(optarg);
283                 if (max_idle < 1 || max_idle > 65535) {
284                     ofp_fatal(0, "--max-idle argument must be between 1 and "
285                               "65535 or the word 'permanent'");
286                 }
287             }
288             break;
289
290         case 'h':
291             usage();
292
293         case 'V':
294             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
295             exit(EXIT_SUCCESS);
296
297         case 'v':
298             vlog_set_verbosity(optarg);
299             break;
300
301         VCONN_SSL_OPTION_HANDLERS
302
303         case '?':
304             exit(EXIT_FAILURE);
305
306         default:
307             abort();
308         }
309     }
310     free(short_options);
311 }
312
313 static void
314 usage(void)
315 {
316     printf("%s: OpenFlow controller\n"
317            "usage: %s [OPTIONS] METHOD\n"
318            "where METHOD is any OpenFlow connection method.\n",
319            program_name, program_name);
320     vconn_usage(true, true);
321     printf("\nOther options:\n"
322            "  -D, --detach            run in background as daemon\n"
323            "  -P, --pidfile[=FILE]    create pidfile (default: %s/controller.pid)\n"
324            "  -f, --force             with -P, start even if already running\n"
325            "  -H, --hub               act as hub instead of learning switch\n"
326            "  -n, --noflow            pass traffic, but don't add flows\n"
327            "  --max-idle=SECS         max idle time for new flows\n"
328            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
329            "  -v, --verbose           set maximum verbosity level\n"
330            "  -h, --help              display this help message\n"
331            "  -V, --version           display version information\n",
332            RUNDIR);
333     exit(EXIT_SUCCESS);
334 }