1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
44 #include "command-line.h"
48 #include "learning-switch.h"
50 #include "poll-loop.h"
54 #include "vconn-ssl.h"
56 #include "vlog-socket.h"
59 #define THIS_MODULE VLM_controller
61 #define MAX_SWITCHES 16
62 #define MAX_LISTENERS 16
65 struct lswitch *lswitch;
69 /* Learn the ports on which MAC addresses appear? */
70 static bool learn_macs = true;
72 /* Set up flows? (If not, every packet is processed at the controller.) */
73 static bool setup_flows = true;
75 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
76 static int max_idle = 60;
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;
84 main(int argc, char *argv[])
86 struct switch_ switches[MAX_SWITCHES];
87 struct vconn *listeners[MAX_LISTENERS];
88 int n_switches, n_listeners;
92 set_program_name(argv[0]);
93 register_fault_handlers();
96 parse_options(argc, argv);
97 signal(SIGPIPE, SIG_IGN);
99 if (argc - optind < 1) {
100 fatal(0, "at least one vconn argument required; use --help for usage");
103 retval = vlog_server_listen(NULL, NULL);
105 fatal(retval, "Could not listen for vlog connections");
108 n_switches = n_listeners = 0;
109 for (i = optind; i < argc; i++) {
110 const char *name = argv[i];
114 retval = vconn_open(name, &vconn);
116 VLOG_ERR("%s: connect: %s", name, strerror(retval));
120 if (vconn_is_passive(vconn)) {
121 if (n_listeners >= MAX_LISTENERS) {
122 fatal(0, "max %d passive connections", n_listeners);
124 listeners[n_listeners++] = vconn;
126 if (n_switches >= MAX_SWITCHES) {
127 fatal(0, "max %d switch connections", n_switches);
129 new_switch(&switches[n_switches++], vconn, name);
132 if (n_switches == 0 && n_listeners == 0) {
133 fatal(0, "no active or passive switch connections");
138 while (n_switches > 0 || n_listeners > 0) {
142 /* Accept connections on listening vconns. */
143 for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
144 struct vconn *new_vconn;
147 retval = vconn_accept(listeners[i], &new_vconn);
148 if (!retval || retval == EAGAIN) {
150 new_switch(&switches[n_switches++], new_vconn, "tcp");
154 vconn_close(listeners[i]);
155 listeners[i] = listeners[--n_listeners];
159 /* Do some switching work. Limit the number of iterations so that
160 * callbacks registered with the poll loop don't starve. */
161 for (iteration = 0; iteration < 50; iteration++) {
162 bool progress = false;
163 for (i = 0; i < n_switches; ) {
164 struct switch_ *this = &switches[i];
165 int retval = do_switching(this);
166 if (!retval || retval == EAGAIN) {
172 lswitch_destroy(this->lswitch);
173 rconn_destroy(this->rconn);
174 switches[i] = switches[--n_switches];
182 /* Wait for something to happen. */
183 if (n_switches < MAX_SWITCHES) {
184 for (i = 0; i < n_listeners; i++) {
185 vconn_accept_wait(listeners[i]);
188 for (i = 0; i < n_switches; i++) {
189 struct switch_ *sw = &switches[i];
190 rconn_run_wait(sw->rconn);
191 rconn_recv_wait(sw->rconn);
200 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
202 sw->rconn = rconn_new_from_vconn(name, vconn);
203 sw->lswitch = lswitch_create(sw->rconn, learn_macs,
204 setup_flows ? max_idle : -1);
208 do_switching(struct switch_ *sw)
210 unsigned int packets_sent;
213 packets_sent = rconn_packets_sent(sw->rconn);
215 msg = rconn_recv(sw->rconn);
217 lswitch_process_packet(sw->lswitch, sw->rconn, msg);
220 rconn_run(sw->rconn);
222 return (!rconn_is_alive(sw->rconn) ? EOF
223 : rconn_packets_sent(sw->rconn) != packets_sent ? 0
228 parse_options(int argc, char *argv[])
230 enum { OPT_MAX_IDLE = UCHAR_MAX + 1 };
231 static struct option long_options[] = {
232 {"detach", no_argument, 0, 'D'},
233 {"pidfile", optional_argument, 0, 'P'},
234 {"hub", no_argument, 0, 'H'},
235 {"noflow", no_argument, 0, 'n'},
236 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
237 {"verbose", optional_argument, 0, 'v'},
238 {"help", no_argument, 0, 'h'},
239 {"version", no_argument, 0, 'V'},
240 VCONN_SSL_LONG_OPTIONS
243 char *short_options = long_options_to_short_options(long_options);
249 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
272 if (!strcmp(optarg, "permanent")) {
273 max_idle = OFP_FLOW_PERMANENT;
275 max_idle = atoi(optarg);
276 if (max_idle < 1 || max_idle > 65535) {
277 fatal(0, "--max-idle argument must be between 1 and "
278 "65535 or the word 'permanent'");
287 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
291 vlog_set_verbosity(optarg);
294 VCONN_SSL_OPTION_HANDLERS
309 printf("%s: OpenFlow controller\n"
310 "usage: %s [OPTIONS] METHOD\n"
311 "where METHOD is any OpenFlow connection method.\n",
312 program_name, program_name);
313 vconn_usage(true, true);
314 printf("\nOther options:\n"
315 " -D, --detach run in background as daemon\n"
316 " -P, --pidfile[=FILE] create pidfile (default: %s/controller.pid)\n"
317 " -H, --hub act as hub instead of learning switch\n"
318 " -n, --noflow pass traffic, but don't add flows\n"
319 " --max-idle=SECS max idle time for new flows\n"
320 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
321 " -v, --verbose set maximum verbosity level\n"
322 " -h, --help display this help message\n"
323 " -V, --version display version information\n",