2 * Copyright (c) 2008, 2009, 2010 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"
29 #include "learning-switch.h"
31 #include "openflow/openflow.h"
32 #include "poll-loop.h"
34 #include "stream-ssl.h"
41 #define THIS_MODULE VLM_controller
43 #define MAX_SWITCHES 16
44 #define MAX_LISTENERS 16
47 struct lswitch *lswitch;
51 /* Learn the ports on which MAC addresses appear? */
52 static bool learn_macs = true;
54 /* Set up flows? (If not, every packet is processed at the controller.) */
55 static bool set_up_flows = true;
57 /* -N, --normal: Use "NORMAL" action instead of explicit port? */
58 static bool action_normal = false;
60 /* -w, --wildcard: Set up exact match or wildcard flow entries? */
61 static bool exact_flows = true;
63 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
64 static int max_idle = 60;
66 /* --mute: If true, accept connections from switches but do not reply to any
67 * of their messages (for debugging fail-open mode). */
68 static bool mute = false;
70 /* --unixctl: Name of unixctl socket, or null to use the default. */
71 static char *unixctl_path = NULL;
73 static int do_switching(struct switch_ *);
74 static void new_switch(struct switch_ *, struct vconn *, const char *name);
75 static void parse_options(int argc, char *argv[]);
76 static void usage(void) NO_RETURN;
79 main(int argc, char *argv[])
81 struct unixctl_server *unixctl;
82 struct switch_ switches[MAX_SWITCHES];
83 struct pvconn *listeners[MAX_LISTENERS];
84 int n_switches, n_listeners;
88 proctitle_init(argc, argv);
89 set_program_name(argv[0]);
92 parse_options(argc, argv);
93 signal(SIGPIPE, SIG_IGN);
95 if (argc - optind < 1) {
96 ovs_fatal(0, "at least one vconn argument required; "
97 "use --help for usage");
100 n_switches = n_listeners = 0;
101 for (i = optind; i < argc; i++) {
102 const char *name = argv[i];
106 retval = vconn_open(name, OFP_VERSION, &vconn);
108 if (n_switches >= MAX_SWITCHES) {
109 ovs_fatal(0, "max %d switch connections", n_switches);
111 new_switch(&switches[n_switches++], vconn, name);
113 } else if (retval == EAFNOSUPPORT) {
114 struct pvconn *pvconn;
115 retval = pvconn_open(name, &pvconn);
117 if (n_listeners >= MAX_LISTENERS) {
118 ovs_fatal(0, "max %d passive connections", n_listeners);
120 listeners[n_listeners++] = pvconn;
124 VLOG_ERR("%s: connect: %s", name, strerror(retval));
127 if (n_switches == 0 && n_listeners == 0) {
128 ovs_fatal(0, "no active or passive switch connections");
131 die_if_already_running();
134 retval = unixctl_server_create(unixctl_path, &unixctl);
139 daemonize_complete();
141 while (n_switches > 0 || n_listeners > 0) {
145 /* Accept connections on listening vconns. */
146 for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
147 struct vconn *new_vconn;
150 retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
151 if (!retval || retval == EAGAIN) {
153 new_switch(&switches[n_switches++], new_vconn, "tcp");
157 pvconn_close(listeners[i]);
158 listeners[i] = listeners[--n_listeners];
162 /* Do some switching work. Limit the number of iterations so that
163 * callbacks registered with the poll loop don't starve. */
164 for (iteration = 0; iteration < 50; iteration++) {
165 bool progress = false;
166 for (i = 0; i < n_switches; ) {
167 struct switch_ *this = &switches[i];
168 int retval = do_switching(this);
169 if (!retval || retval == EAGAIN) {
175 rconn_destroy(this->rconn);
176 lswitch_destroy(this->lswitch);
177 switches[i] = switches[--n_switches];
184 for (i = 0; i < n_switches; i++) {
185 struct switch_ *this = &switches[i];
186 lswitch_run(this->lswitch, this->rconn);
189 unixctl_server_run(unixctl);
191 /* Wait for something to happen. */
192 if (n_switches < MAX_SWITCHES) {
193 for (i = 0; i < n_listeners; i++) {
194 pvconn_wait(listeners[i]);
197 for (i = 0; i < n_switches; i++) {
198 struct switch_ *sw = &switches[i];
199 rconn_run_wait(sw->rconn);
200 rconn_recv_wait(sw->rconn);
201 lswitch_wait(sw->lswitch);
203 unixctl_server_wait(unixctl);
211 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
213 sw->rconn = rconn_new_from_vconn(name, vconn);
214 sw->lswitch = lswitch_create(sw->rconn, learn_macs, exact_flows,
215 set_up_flows ? max_idle : -1,
220 do_switching(struct switch_ *sw)
222 unsigned int packets_sent;
225 packets_sent = rconn_packets_sent(sw->rconn);
227 msg = rconn_recv(sw->rconn);
230 lswitch_process_packet(sw->lswitch, sw->rconn, msg);
234 rconn_run(sw->rconn);
236 return (!rconn_is_alive(sw->rconn) ? EOF
237 : rconn_packets_sent(sw->rconn) != packets_sent ? 0
242 parse_options(int argc, char *argv[])
245 OPT_MAX_IDLE = UCHAR_MAX + 1,
251 static struct option long_options[] = {
252 {"hub", no_argument, 0, 'H'},
253 {"noflow", no_argument, 0, 'n'},
254 {"normal", no_argument, 0, 'N'},
255 {"wildcard", no_argument, 0, 'w'},
256 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
257 {"mute", no_argument, 0, OPT_MUTE},
258 {"unixctl", required_argument, 0, OPT_UNIXCTL},
259 {"help", no_argument, 0, 'h'},
260 {"version", no_argument, 0, 'V'},
264 STREAM_SSL_LONG_OPTIONS
265 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
269 char *short_options = long_options_to_short_options(long_options);
275 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
286 set_up_flows = false;
294 action_normal = true;
302 if (!strcmp(optarg, "permanent")) {
303 max_idle = OFP_FLOW_PERMANENT;
305 max_idle = atoi(optarg);
306 if (max_idle < 1 || max_idle > 65535) {
307 ovs_fatal(0, "--max-idle argument must be between 1 and "
308 "65535 or the word 'permanent'");
314 unixctl_path = optarg;
321 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
325 DAEMON_OPTION_HANDLERS
328 STREAM_SSL_OPTION_HANDLERS
330 case OPT_PEER_CA_CERT:
331 stream_ssl_set_peer_ca_cert_file(optarg);
348 printf("%s: OpenFlow controller\n"
349 "usage: %s [OPTIONS] METHOD\n"
350 "where METHOD is any OpenFlow connection method.\n",
351 program_name, program_name);
352 vconn_usage(true, true, false);
355 printf("\nOther options:\n"
356 " -H, --hub act as hub instead of learning switch\n"
357 " -n, --noflow pass traffic, but don't add flows\n"
358 " --max-idle=SECS max idle time for new flows\n"
359 " -N, --normal use OFPAT_NORMAL action\n"
360 " -w, --wildcard use wildcards, not exact-match rules\n"
361 " --unixctl=SOCKET override default control socket name\n"
362 " -h, --help display this help message\n"
363 " -V, --version display version information\n");