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