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.
27 #include "command-line.h"
30 #include "learning-switch.h"
31 #include "ofp-parse.h"
33 #include "openflow/openflow.h"
34 #include "poll-loop.h"
36 #include "stream-ssl.h"
43 VLOG_DEFINE_THIS_MODULE(controller)
45 #define MAX_SWITCHES 16
46 #define MAX_LISTENERS 16
49 struct lswitch *lswitch;
53 /* Learn the ports on which MAC addresses appear? */
54 static bool learn_macs = true;
56 /* Set up flows? (If not, every packet is processed at the controller.) */
57 static bool set_up_flows = true;
59 /* -N, --normal: Use "NORMAL" action instead of explicit port? */
60 static bool action_normal = false;
62 /* -w, --wildcard: Set up exact match or wildcard flow entries? */
63 static bool exact_flows = true;
65 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
66 static int max_idle = 60;
68 /* --mute: If true, accept connections from switches but do not reply to any
69 * of their messages (for debugging fail-open mode). */
70 static bool mute = false;
72 /* -q, --queue: OpenFlow queue to use, or the default queue if UINT32_MAX. */
73 static uint32_t queue_id = UINT32_MAX;
75 /* --with-flows: File with flows to send to switch, or null to not load
76 * any default flows. */
77 static struct ovs_queue default_flows = OVS_QUEUE_INITIALIZER;
79 /* --unixctl: Name of unixctl socket, or null to use the default. */
80 static char *unixctl_path = NULL;
82 static int do_switching(struct switch_ *);
83 static void new_switch(struct switch_ *, struct vconn *);
84 static void parse_options(int argc, char *argv[]);
85 static void usage(void) NO_RETURN;
88 main(int argc, char *argv[])
90 struct unixctl_server *unixctl;
91 struct switch_ switches[MAX_SWITCHES];
92 struct pvconn *listeners[MAX_LISTENERS];
93 int n_switches, n_listeners;
97 proctitle_init(argc, argv);
98 set_program_name(argv[0]);
99 parse_options(argc, argv);
100 signal(SIGPIPE, SIG_IGN);
102 if (argc - optind < 1) {
103 ovs_fatal(0, "at least one vconn argument required; "
104 "use --help for usage");
107 n_switches = n_listeners = 0;
108 for (i = optind; i < argc; i++) {
109 const char *name = argv[i];
112 retval = vconn_open(name, OFP_VERSION, &vconn);
114 if (n_switches >= MAX_SWITCHES) {
115 ovs_fatal(0, "max %d switch connections", n_switches);
117 new_switch(&switches[n_switches++], vconn);
119 } else if (retval == EAFNOSUPPORT) {
120 struct pvconn *pvconn;
121 retval = pvconn_open(name, &pvconn);
123 if (n_listeners >= MAX_LISTENERS) {
124 ovs_fatal(0, "max %d passive connections", n_listeners);
126 listeners[n_listeners++] = pvconn;
130 VLOG_ERR("%s: connect: %s", name, strerror(retval));
133 if (n_switches == 0 && n_listeners == 0) {
134 ovs_fatal(0, "no active or passive switch connections");
137 die_if_already_running();
140 retval = unixctl_server_create(unixctl_path, &unixctl);
145 daemonize_complete();
147 while (n_switches > 0 || n_listeners > 0) {
150 /* Accept connections on listening vconns. */
151 for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
152 struct vconn *new_vconn;
154 retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
155 if (!retval || retval == EAGAIN) {
157 new_switch(&switches[n_switches++], new_vconn);
161 pvconn_close(listeners[i]);
162 listeners[i] = listeners[--n_listeners];
166 /* Do some switching work. Limit the number of iterations so that
167 * callbacks registered with the poll loop don't starve. */
168 for (iteration = 0; iteration < 50; iteration++) {
169 bool progress = false;
170 for (i = 0; i < n_switches; ) {
171 struct switch_ *this = &switches[i];
173 retval = do_switching(this);
174 if (!retval || retval == EAGAIN) {
180 rconn_destroy(this->rconn);
181 lswitch_destroy(this->lswitch);
182 switches[i] = switches[--n_switches];
189 for (i = 0; i < n_switches; i++) {
190 struct switch_ *this = &switches[i];
191 lswitch_run(this->lswitch);
194 unixctl_server_run(unixctl);
196 /* Wait for something to happen. */
197 if (n_switches < MAX_SWITCHES) {
198 for (i = 0; i < n_listeners; i++) {
199 pvconn_wait(listeners[i]);
202 for (i = 0; i < n_switches; i++) {
203 struct switch_ *sw = &switches[i];
204 rconn_run_wait(sw->rconn);
205 rconn_recv_wait(sw->rconn);
206 lswitch_wait(sw->lswitch);
208 unixctl_server_wait(unixctl);
216 new_switch(struct switch_ *sw, struct vconn *vconn)
218 struct lswitch_config cfg;
220 sw->rconn = rconn_create(60, 0);
221 rconn_connect_unreliably(sw->rconn, vconn, NULL);
223 cfg.mode = (action_normal ? LSW_NORMAL
224 : learn_macs ? LSW_LEARN
226 cfg.max_idle = set_up_flows ? max_idle : -1;
227 cfg.default_flows = default_flows.head;
228 cfg.queue_id = queue_id;
229 sw->lswitch = lswitch_create(sw->rconn, &cfg);
233 do_switching(struct switch_ *sw)
235 unsigned int packets_sent;
238 packets_sent = rconn_packets_sent(sw->rconn);
240 msg = rconn_recv(sw->rconn);
243 lswitch_process_packet(sw->lswitch, sw->rconn, msg);
247 rconn_run(sw->rconn);
249 return (!rconn_is_alive(sw->rconn) ? EOF
250 : rconn_packets_sent(sw->rconn) != packets_sent ? 0
255 read_flow_file(const char *name)
260 stream = fopen(optarg, "r");
262 ovs_fatal(errno, "%s: open", name);
265 while ((b = parse_ofp_add_flow_file(stream)) != NULL) {
266 queue_push_tail(&default_flows, b);
273 parse_options(int argc, char *argv[])
276 OPT_MAX_IDLE = UCHAR_MAX + 1,
283 static struct option long_options[] = {
284 {"hub", no_argument, 0, 'H'},
285 {"noflow", no_argument, 0, 'n'},
286 {"normal", no_argument, 0, 'N'},
287 {"wildcard", no_argument, 0, 'w'},
288 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
289 {"mute", no_argument, 0, OPT_MUTE},
290 {"queue", required_argument, 0, 'q'},
291 {"with-flows", required_argument, 0, OPT_WITH_FLOWS},
292 {"unixctl", required_argument, 0, OPT_UNIXCTL},
293 {"help", no_argument, 0, 'h'},
294 {"version", no_argument, 0, 'V'},
298 STREAM_SSL_LONG_OPTIONS
299 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
303 char *short_options = long_options_to_short_options(long_options);
309 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
320 set_up_flows = false;
328 action_normal = true;
336 if (!strcmp(optarg, "permanent")) {
337 max_idle = OFP_FLOW_PERMANENT;
339 max_idle = atoi(optarg);
340 if (max_idle < 1 || max_idle > 65535) {
341 ovs_fatal(0, "--max-idle argument must be between 1 and "
342 "65535 or the word 'permanent'");
348 queue_id = atoi(optarg);
352 read_flow_file(optarg);
356 unixctl_path = optarg;
363 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
367 DAEMON_OPTION_HANDLERS
370 STREAM_SSL_OPTION_HANDLERS
372 case OPT_PEER_CA_CERT:
373 stream_ssl_set_peer_ca_cert_file(optarg);
390 printf("%s: OpenFlow controller\n"
391 "usage: %s [OPTIONS] METHOD\n"
392 "where METHOD is any OpenFlow connection method.\n",
393 program_name, program_name);
394 vconn_usage(true, true, false);
397 printf("\nOther options:\n"
398 " -H, --hub act as hub instead of learning switch\n"
399 " -n, --noflow pass traffic, but don't add flows\n"
400 " --max-idle=SECS max idle time for new flows\n"
401 " -N, --normal use OFPAT_NORMAL action\n"
402 " -w, --wildcard use wildcards, not exact-match rules\n"
403 " -q, --queue=QUEUE OpenFlow queue ID to use for output\n"
404 " --with-flows FILE use the flows from FILE\n"
405 " --unixctl=SOCKET override default control socket name\n"
406 " -h, --help display this help message\n"
407 " -V, --version display version information\n");