2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 #include "command-line.h"
30 #include "learning-switch.h"
32 #include "openflow/openflow.h"
33 #include "poll-loop.h"
38 #include "vconn-ssl.h"
42 #define THIS_MODULE VLM_controller
44 #define MAX_SWITCHES 16
45 #define MAX_LISTENERS 16
48 struct lswitch *lswitch;
52 /* Learn the ports on which MAC addresses appear? */
53 static bool learn_macs = true;
55 /* Set up flows? (If not, every packet is processed at the controller.) */
56 static bool setup_flows = true;
58 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
59 static int max_idle = 60;
61 static int do_switching(struct switch_ *);
62 static void new_switch(struct switch_ *, struct vconn *, const char *name);
63 static void parse_options(int argc, char *argv[]);
64 static void usage(void) NO_RETURN;
67 main(int argc, char *argv[])
69 struct unixctl_server *unixctl;
70 struct switch_ switches[MAX_SWITCHES];
71 struct pvconn *listeners[MAX_LISTENERS];
72 int n_switches, n_listeners;
76 set_program_name(argv[0]);
77 register_fault_handlers();
80 parse_options(argc, argv);
81 signal(SIGPIPE, SIG_IGN);
83 if (argc - optind < 1) {
84 ovs_fatal(0, "at least one vconn argument required; "
85 "use --help for usage");
88 n_switches = n_listeners = 0;
89 for (i = optind; i < argc; i++) {
90 const char *name = argv[i];
94 retval = vconn_open(name, OFP_VERSION, &vconn);
96 if (n_switches >= MAX_SWITCHES) {
97 ovs_fatal(0, "max %d switch connections", n_switches);
99 new_switch(&switches[n_switches++], vconn, name);
101 } else if (retval == EAFNOSUPPORT) {
102 struct pvconn *pvconn;
103 retval = pvconn_open(name, &pvconn);
105 if (n_listeners >= MAX_LISTENERS) {
106 ovs_fatal(0, "max %d passive connections", n_listeners);
108 listeners[n_listeners++] = pvconn;
112 VLOG_ERR("%s: connect: %s", name, strerror(retval));
115 if (n_switches == 0 && n_listeners == 0) {
116 ovs_fatal(0, "no active or passive switch connections");
119 die_if_already_running();
122 retval = unixctl_server_create(NULL, &unixctl);
124 ovs_fatal(retval, "Could not listen for unixctl connections");
127 while (n_switches > 0 || n_listeners > 0) {
131 /* Accept connections on listening vconns. */
132 for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
133 struct vconn *new_vconn;
136 retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
137 if (!retval || retval == EAGAIN) {
139 new_switch(&switches[n_switches++], new_vconn, "tcp");
143 pvconn_close(listeners[i]);
144 listeners[i] = listeners[--n_listeners];
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) {
161 rconn_destroy(this->rconn);
162 lswitch_destroy(this->lswitch);
163 switches[i] = switches[--n_switches];
170 for (i = 0; i < n_switches; i++) {
171 struct switch_ *this = &switches[i];
172 lswitch_run(this->lswitch, this->rconn);
175 unixctl_server_run(unixctl);
177 /* Wait for something to happen. */
178 if (n_switches < MAX_SWITCHES) {
179 for (i = 0; i < n_listeners; i++) {
180 pvconn_wait(listeners[i]);
183 for (i = 0; i < n_switches; i++) {
184 struct switch_ *sw = &switches[i];
185 rconn_run_wait(sw->rconn);
186 rconn_recv_wait(sw->rconn);
187 lswitch_wait(sw->lswitch);
189 unixctl_server_wait(unixctl);
197 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
199 sw->rconn = rconn_new_from_vconn(name, vconn);
200 sw->lswitch = lswitch_create(sw->rconn, learn_macs,
201 setup_flows ? max_idle : -1);
205 do_switching(struct switch_ *sw)
207 unsigned int packets_sent;
210 packets_sent = rconn_packets_sent(sw->rconn);
212 msg = rconn_recv(sw->rconn);
214 lswitch_process_packet(sw->lswitch, sw->rconn, msg);
217 rconn_run(sw->rconn);
219 return (!rconn_is_alive(sw->rconn) ? EOF
220 : rconn_packets_sent(sw->rconn) != packets_sent ? 0
225 parse_options(int argc, char *argv[])
228 OPT_MAX_IDLE = UCHAR_MAX + 1,
232 static struct option long_options[] = {
233 {"hub", no_argument, 0, 'H'},
234 {"noflow", no_argument, 0, 'n'},
235 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
236 {"help", no_argument, 0, 'h'},
237 {"version", no_argument, 0, 'V'},
241 VCONN_SSL_LONG_OPTIONS
242 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
246 char *short_options = long_options_to_short_options(long_options);
252 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
267 if (!strcmp(optarg, "permanent")) {
268 max_idle = OFP_FLOW_PERMANENT;
270 max_idle = atoi(optarg);
271 if (max_idle < 1 || max_idle > 65535) {
272 ovs_fatal(0, "--max-idle argument must be between 1 and "
273 "65535 or the word 'permanent'");
282 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
286 DAEMON_OPTION_HANDLERS
289 VCONN_SSL_OPTION_HANDLERS
291 case OPT_PEER_CA_CERT:
292 vconn_ssl_set_peer_ca_cert_file(optarg);
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, false);
316 printf("\nOther options:\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 " -h, --help display this help message\n"
321 " -V, --version display version information\n");