2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015 Nicira, Inc.
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 "fatal-signal.h"
31 #include "learning-switch.h"
32 #include "ofp-parse.h"
33 #include "ofp-version-opt.h"
35 #include "openflow/openflow.h"
36 #include "poll-loop.h"
39 #include "stream-ssl.h"
43 #include "openvswitch/vconn.h"
44 #include "openvswitch/vlog.h"
45 #include "socket-util.h"
48 VLOG_DEFINE_THIS_MODULE(controller);
50 #define MAX_SWITCHES 16
51 #define MAX_LISTENERS 16
54 struct lswitch *lswitch;
57 /* -H, --hub: Learn the ports on which MAC addresses appear? */
58 static bool learn_macs = true;
60 /* -n, --noflow: Set up flows? (If not, every packet is processed at the
62 static bool set_up_flows = true;
64 /* -N, --normal: Use "NORMAL" action instead of explicit port? */
65 static bool action_normal = false;
67 /* -w, --wildcard: 0 to disable wildcard flow entries, an OFPFW10_* bitmask to
68 * enable specific wildcards, or UINT32_MAX to use the default wildcards. */
69 static uint32_t wildcards = 0;
71 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
72 static int max_idle = 60;
74 /* --mute: If true, accept connections from switches but do not reply to any
75 * of their messages (for debugging fail-open mode). */
76 static bool mute = false;
78 /* -q, --queue: default OpenFlow queue, none if UINT32_MAX. */
79 static uint32_t default_queue = UINT32_MAX;
81 /* -Q, --port-queue: map from port name to port number. */
82 static struct simap port_queues = SIMAP_INITIALIZER(&port_queues);
84 /* --with-flows: Flows to send to switch. */
85 static struct ofputil_flow_mod *default_flows;
86 static size_t n_default_flows;
87 static enum ofputil_protocol usable_protocols;
89 /* --unixctl: Name of unixctl socket, or null to use the default. */
90 static char *unixctl_path = NULL;
92 static void new_switch(struct switch_ *, struct vconn *);
93 static void parse_options(int argc, char *argv[]);
94 OVS_NO_RETURN static void usage(void);
97 main(int argc, char *argv[])
99 struct unixctl_server *unixctl;
100 struct switch_ switches[MAX_SWITCHES];
101 struct pvconn *listeners[MAX_LISTENERS];
102 int n_switches, n_listeners;
106 ovs_cmdl_proctitle_init(argc, argv);
107 set_program_name(argv[0]);
108 parse_options(argc, argv);
109 fatal_ignore_sigpipe();
111 daemon_become_new_user(false);
113 if (argc - optind < 1) {
114 ovs_fatal(0, "at least one vconn argument required; "
115 "use --help for usage");
118 n_switches = n_listeners = 0;
119 for (i = optind; i < argc; i++) {
120 const char *name = argv[i];
123 retval = vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT,
126 if (n_switches >= MAX_SWITCHES) {
127 ovs_fatal(0, "max %d switch connections", n_switches);
129 new_switch(&switches[n_switches++], vconn);
131 } else if (retval == EAFNOSUPPORT) {
132 struct pvconn *pvconn;
133 retval = pvconn_open(name, get_allowed_ofp_versions(),
134 DSCP_DEFAULT, &pvconn);
136 if (n_listeners >= MAX_LISTENERS) {
137 ovs_fatal(0, "max %d passive connections", n_listeners);
139 listeners[n_listeners++] = pvconn;
143 VLOG_ERR("%s: connect: %s", name, ovs_strerror(retval));
146 if (n_switches == 0 && n_listeners == 0) {
147 ovs_fatal(0, "no active or passive switch connections");
150 daemonize_start(false);
152 retval = unixctl_server_create(unixctl_path, &unixctl);
157 daemonize_complete();
159 while (n_switches > 0 || n_listeners > 0) {
160 /* Accept connections on listening vconns. */
161 for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
162 struct vconn *new_vconn;
164 retval = pvconn_accept(listeners[i], &new_vconn);
165 if (!retval || retval == EAGAIN) {
167 new_switch(&switches[n_switches++], new_vconn);
171 pvconn_close(listeners[i]);
172 listeners[i] = listeners[--n_listeners];
176 /* Do some switching work. . */
177 for (i = 0; i < n_switches; ) {
178 struct switch_ *this = &switches[i];
179 lswitch_run(this->lswitch);
180 if (lswitch_is_alive(this->lswitch)) {
183 lswitch_destroy(this->lswitch);
184 switches[i] = switches[--n_switches];
188 unixctl_server_run(unixctl);
190 /* Wait for something to happen. */
191 if (n_switches < MAX_SWITCHES) {
192 for (i = 0; i < n_listeners; i++) {
193 pvconn_wait(listeners[i]);
196 for (i = 0; i < n_switches; i++) {
197 struct switch_ *sw = &switches[i];
198 lswitch_wait(sw->lswitch);
200 unixctl_server_wait(unixctl);
208 new_switch(struct switch_ *sw, struct vconn *vconn)
210 struct lswitch_config cfg;
213 rconn = rconn_create(60, 0, DSCP_DEFAULT, get_allowed_ofp_versions());
214 rconn_connect_unreliably(rconn, vconn, NULL);
216 cfg.mode = (action_normal ? LSW_NORMAL
217 : learn_macs ? LSW_LEARN
219 cfg.wildcards = wildcards;
220 cfg.max_idle = set_up_flows ? max_idle : -1;
221 cfg.default_flows = default_flows;
222 cfg.n_default_flows = n_default_flows;
223 cfg.usable_protocols = usable_protocols;
224 cfg.default_queue = default_queue;
225 cfg.port_queues = &port_queues;
227 sw->lswitch = lswitch_create(rconn, &cfg);
231 add_port_queue(char *s)
233 char *save_ptr = NULL;
237 port_name = strtok_r(s, ":", &save_ptr);
238 queue_id = strtok_r(NULL, "", &save_ptr);
240 ovs_fatal(0, "argument to -Q or --port-queue should take the form "
241 "\"<port-name>:<queue-id>\"");
244 if (!simap_put(&port_queues, port_name, atoi(queue_id))) {
245 ovs_fatal(0, "<port-name> arguments for -Q or --port-queue must "
251 parse_options(int argc, char *argv[])
254 OPT_MAX_IDLE = UCHAR_MAX + 1,
261 OFP_VERSION_OPTION_ENUMS
263 static const struct option long_options[] = {
264 {"hub", no_argument, NULL, 'H'},
265 {"noflow", no_argument, NULL, 'n'},
266 {"normal", no_argument, NULL, 'N'},
267 {"wildcards", optional_argument, NULL, 'w'},
268 {"max-idle", required_argument, NULL, OPT_MAX_IDLE},
269 {"mute", no_argument, NULL, OPT_MUTE},
270 {"queue", required_argument, NULL, 'q'},
271 {"port-queue", required_argument, NULL, 'Q'},
272 {"with-flows", required_argument, NULL, OPT_WITH_FLOWS},
273 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
274 {"help", no_argument, NULL, 'h'},
276 OFP_VERSION_LONG_OPTIONS,
278 STREAM_SSL_LONG_OPTIONS,
279 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
282 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
289 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
300 set_up_flows = false;
308 action_normal = true;
312 wildcards = optarg ? strtol(optarg, NULL, 16) : UINT32_MAX;
316 if (!strcmp(optarg, "permanent")) {
317 max_idle = OFP_FLOW_PERMANENT;
319 max_idle = atoi(optarg);
320 if (max_idle < 1 || max_idle > 65535) {
321 ovs_fatal(0, "--max-idle argument must be between 1 and "
322 "65535 or the word 'permanent'");
328 default_queue = atoi(optarg);
332 add_port_queue(optarg);
336 error = parse_ofp_flow_mod_file(optarg, OFPFC_ADD, &default_flows,
340 ovs_fatal(0, "%s", error);
345 unixctl_path = optarg;
352 OFP_VERSION_OPTION_HANDLERS
353 DAEMON_OPTION_HANDLERS
355 STREAM_SSL_OPTION_HANDLERS
357 case OPT_PEER_CA_CERT:
358 stream_ssl_set_peer_ca_cert_file(optarg);
370 if (!simap_is_empty(&port_queues) || default_queue != UINT32_MAX) {
372 ovs_error(0, "queue IDs are incompatible with -N or --normal; "
373 "not using OFPP_NORMAL");
374 action_normal = false;
378 ovs_error(0, "queue IDs are incompatible with -H or --hub; "
379 "not acting as hub");
388 printf("%s: OpenFlow controller\n"
389 "usage: %s [OPTIONS] METHOD\n"
390 "where METHOD is any OpenFlow connection method.\n",
391 program_name, program_name);
392 vconn_usage(true, true, false);
396 printf("\nOther options:\n"
397 " -H, --hub act as hub instead of learning switch\n"
398 " -n, --noflow pass traffic, but don't add flows\n"
399 " --max-idle=SECS max idle time for new flows\n"
400 " -N, --normal use OFPP_NORMAL action\n"
401 " -w, --wildcards[=MASK] wildcard (specified) bits in flows\n"
402 " -q, --queue=QUEUE-ID OpenFlow queue ID to use for output\n"
403 " -Q PORT-NAME:QUEUE-ID use QUEUE-ID for frames from PORT-NAME\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");