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.
22 #include <netinet/in.h>
27 #include "command-line.h"
32 #include "leak-checker.h"
36 #include "ofproto/ofproto.h"
37 #include "openflow/openflow.h"
39 #include "poll-loop.h"
41 #include "stream-ssl.h"
49 #define THIS_MODULE VLM_openflowd
51 /* Behavior when the connection to the controller fails. */
53 FAIL_OPEN, /* Act as learning switch. */
54 FAIL_CLOSED /* Drop all packets. */
57 /* Settings that may be configured by the user. */
59 /* Overall mode of operation. */
60 bool discovery; /* Discover the controller automatically? */
61 bool in_band; /* Connect to controller in-band? */
64 uint64_t datapath_id; /* Datapath ID. */
65 char *dp_name; /* Name of local datapath. */
66 char *dp_type; /* Type of local datapath. */
67 struct svec ports; /* Set of ports to add to datapath (if any). */
69 /* Description strings. */
70 const char *mfr_desc; /* Manufacturer. */
71 const char *hw_desc; /* Hardware. */
72 const char *sw_desc; /* Software version. */
73 const char *serial_desc; /* Serial number. */
75 /* Related vconns and network devices. */
76 const char *controller_name; /* Controller (if not discovery mode). */
77 struct svec listeners; /* Listen for management connections. */
78 struct svec snoops; /* Listen for controller snooping conns. */
80 /* Failure behavior. */
81 enum fail_mode fail_mode; /* Act as learning switch if no controller? */
82 int max_idle; /* Idle time for flows in fail-open mode. */
83 int probe_interval; /* # seconds idle before sending echo request. */
84 int max_backoff; /* Max # seconds between connection attempts. */
86 /* Packet-in rate-limiting. */
87 int rate_limit; /* Tokens added to bucket per second. */
88 int burst_limit; /* Maximum number token bucket size. */
90 /* Discovery behavior. */
91 const char *accept_controller_re; /* Controller vconns to accept. */
92 bool update_resolv_conf; /* Update /etc/resolv.conf? */
94 /* Spanning tree protocol. */
98 struct svec netflow; /* NetFlow targets. */
101 static void parse_options(int argc, char *argv[], struct ofsettings *);
102 static void usage(void) NO_RETURN;
105 main(int argc, char *argv[])
107 struct unixctl_server *unixctl;
108 struct ofproto *ofproto;
112 struct netflow_options nf_options;
114 proctitle_init(argc, argv);
115 set_program_name(argv[0]);
118 parse_options(argc, argv, &s);
119 signal(SIGPIPE, SIG_IGN);
121 die_if_already_running();
124 /* Start listening for ovs-appctl requests. */
125 error = unixctl_server_create(NULL, &unixctl);
130 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
131 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
133 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
135 ovs_fatal(error, "could not create datapath");
138 /* Add ports to the datapath if requested by the user. */
142 struct netdev *netdev;
144 SVEC_FOR_EACH (i, port, &s.ports) {
145 error = netdev_open_default(port, &netdev);
147 ovs_fatal(error, "failed to open %s as a device", port);
150 error = dpif_port_add(dpif, port, 0, NULL);
152 ovs_fatal(error, "failed to add %s as a port", port);
157 /* Start OpenFlow processing. */
158 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
160 ovs_fatal(error, "could not initialize openflow switch");
162 error = ofproto_set_in_band(ofproto, s.in_band);
164 ovs_fatal(error, "failed to configure in-band control");
166 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
167 s.update_resolv_conf);
169 ovs_fatal(error, "failed to configure controller discovery");
172 ofproto_set_datapath_id(ofproto, s.datapath_id);
174 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc, s.serial_desc);
175 if (!s.listeners.n) {
176 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
177 ovs_rundir, s.dp_name));
178 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
179 svec_clear(&s.listeners);
181 error = ofproto_set_listeners(ofproto, &s.listeners);
183 ovs_fatal(error, "failed to configure management connections");
185 error = ofproto_set_snoops(ofproto, &s.snoops);
188 "failed to configure controller snooping connections");
190 memset(&nf_options, 0, sizeof nf_options);
191 nf_options.collectors = s.netflow;
192 error = ofproto_set_netflow(ofproto, &nf_options);
194 ovs_fatal(error, "failed to configure NetFlow collectors");
196 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
197 ofproto_set_probe_interval(ofproto, s.probe_interval);
198 ofproto_set_max_backoff(ofproto, s.max_backoff);
199 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
200 error = ofproto_set_stp(ofproto, s.enable_stp);
202 ovs_fatal(error, "failed to configure STP");
205 error = ofproto_set_controller(ofproto, s.controller_name);
207 ovs_fatal(error, "failed to configure controller");
211 daemonize_complete();
213 while (ofproto_is_alive(ofproto)) {
214 error = ofproto_run(ofproto);
216 ovs_fatal(error, "unrecoverable datapath error");
218 unixctl_server_run(unixctl);
222 ofproto_wait(ofproto);
223 unixctl_server_wait(unixctl);
234 /* User interface. */
237 parse_options(int argc, char *argv[], struct ofsettings *s)
240 OPT_DATAPATH_ID = UCHAR_MAX + 1,
249 OPT_INACTIVITY_PROBE,
255 OPT_BOOTSTRAP_CA_CERT,
264 LEAK_CHECKER_OPTION_ENUMS
266 static struct option long_options[] = {
267 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
268 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
269 {"hardware", required_argument, 0, OPT_HARDWARE},
270 {"software", required_argument, 0, OPT_SOFTWARE},
271 {"serial", required_argument, 0, OPT_SERIAL},
272 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
273 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
274 {"config", required_argument, 0, 'F'},
275 {"br-name", required_argument, 0, OPT_BR_NAME},
276 {"fail", required_argument, 0, OPT_FAIL_MODE},
277 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
278 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
279 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
280 {"listen", required_argument, 0, 'l'},
281 {"snoop", required_argument, 0, OPT_SNOOP},
282 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
283 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
284 {"stp", no_argument, 0, OPT_STP},
285 {"no-stp", no_argument, 0, OPT_NO_STP},
286 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
287 {"in-band", no_argument, 0, OPT_IN_BAND},
288 {"netflow", required_argument, 0, OPT_NETFLOW},
289 {"ports", required_argument, 0, OPT_PORTS},
290 {"verbose", optional_argument, 0, 'v'},
291 {"help", no_argument, 0, 'h'},
292 {"version", no_argument, 0, 'V'},
295 LEAK_CHECKER_LONG_OPTIONS,
297 STREAM_SSL_LONG_OPTIONS
298 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
302 char *short_options = long_options_to_short_options(long_options);
304 /* Set defaults that we can figure out before parsing options. */
309 s->serial_desc = NULL;
310 svec_init(&s->listeners);
311 svec_init(&s->snoops);
312 s->fail_mode = FAIL_OPEN;
314 s->probe_interval = 0;
316 s->update_resolv_conf = true;
319 s->accept_controller_re = NULL;
320 s->enable_stp = false;
322 svec_init(&s->netflow);
323 svec_init(&s->ports);
327 c = getopt_long(argc, argv, short_options, long_options, NULL);
333 case OPT_DATAPATH_ID:
334 if (!dpid_from_string(optarg, &s->datapath_id)) {
335 ovs_fatal(0, "argument to --datapath-id must be "
336 "exactly 12 hex digits and may not be all-zero");
340 case OPT_MANUFACTURER:
341 s->mfr_desc = optarg;
353 s->serial_desc = optarg;
356 case OPT_ACCEPT_VCONN:
357 s->accept_controller_re = optarg;
360 case OPT_NO_RESOLV_CONF:
361 s->update_resolv_conf = false;
365 if (!strcmp(optarg, "open")) {
366 s->fail_mode = FAIL_OPEN;
367 } else if (!strcmp(optarg, "closed")) {
368 s->fail_mode = FAIL_CLOSED;
370 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
374 case OPT_INACTIVITY_PROBE:
375 s->probe_interval = atoi(optarg);
376 if (s->probe_interval < 5) {
377 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
382 if (!strcmp(optarg, "permanent")) {
383 s->max_idle = OFP_FLOW_PERMANENT;
385 s->max_idle = atoi(optarg);
386 if (s->max_idle < 1 || s->max_idle > 65535) {
387 ovs_fatal(0, "--max-idle argument must be between 1 and "
388 "65535 or the word 'permanent'");
393 case OPT_MAX_BACKOFF:
394 s->max_backoff = atoi(optarg);
395 if (s->max_backoff < 1) {
396 ovs_fatal(0, "--max-backoff argument must be at least 1");
397 } else if (s->max_backoff > 3600) {
398 s->max_backoff = 3600;
404 s->rate_limit = atoi(optarg);
405 if (s->rate_limit < 1) {
406 ovs_fatal(0, "--rate-limit argument must be at least 1");
409 s->rate_limit = 1000;
413 case OPT_BURST_LIMIT:
414 s->burst_limit = atoi(optarg);
415 if (s->burst_limit < 1) {
416 ovs_fatal(0, "--burst-limit argument must be at least 1");
421 s->enable_stp = true;
425 s->enable_stp = false;
428 case OPT_OUT_OF_BAND:
437 svec_add(&s->netflow, optarg);
441 svec_add(&s->listeners, optarg);
445 svec_add(&s->snoops, optarg);
449 svec_split(&s->ports, optarg, ",");
456 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
459 DAEMON_OPTION_HANDLERS
463 LEAK_CHECKER_OPTION_HANDLERS
466 STREAM_SSL_OPTION_HANDLERS
468 case OPT_BOOTSTRAP_CA_CERT:
469 stream_ssl_set_ca_cert_file(optarg, true);
484 if (argc < 1 || argc > 2) {
485 ovs_fatal(0, "need one or two non-option arguments; "
486 "use --help for usage");
489 /* Local and remote vconns. */
490 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
492 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
494 /* Set accept_controller_regex. */
495 if (!s->accept_controller_re) {
496 s->accept_controller_re
497 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
500 /* Mode of operation. */
501 s->discovery = s->controller_name == NULL;
502 if (s->discovery && !s->in_band) {
503 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
507 if (s->rate_limit && s->rate_limit < 100) {
508 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
515 printf("%s: an OpenFlow switch implementation.\n"
516 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
517 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
518 "CONTROLLER is an active OpenFlow connection method; if it is\n"
519 "omitted, then ovs-openflowd performs controller discovery.\n",
520 program_name, program_name);
521 vconn_usage(true, true, true);
522 printf("\nOpenFlow options:\n"
523 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
524 " (ID must consist of 12 hex digits)\n"
525 " --manufacturer=MFR Identify manufacturer as MFR\n"
526 " --hardware=HW Identify hardware as HW\n"
527 " --software=SW Identify software as SW\n"
528 " --serial=SERIAL Identify serial number as SERIAL\n"
529 "\nController discovery options:\n"
530 " --accept-vconn=REGEX accept matching discovered controllers\n"
531 " --no-resolv-conf do not update /etc/resolv.conf\n"
532 "\nNetworking options:\n"
533 " --fail=open|closed when controller connection fails:\n"
534 " closed: drop all packets\n"
535 " open (default): act as learning switch\n"
536 " --inactivity-probe=SECS time between inactivity probes\n"
537 " --max-idle=SECS max idle for flows set up by switch\n"
538 " --max-backoff=SECS max time between controller connection\n"
539 " attempts (default: 8 seconds)\n"
540 " -l, --listen=METHOD allow management connections on METHOD\n"
541 " (a passive OpenFlow connection method)\n"
542 " --snoop=METHOD allow controller snooping on METHOD\n"
543 " (a passive OpenFlow connection method)\n"
544 " --out-of-band controller connection is out-of-band\n"
545 " --netflow=HOST:PORT configure NetFlow output target\n"
546 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
547 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
548 " --burst-limit=BURST limit on packet credit for idle time\n");
551 printf("\nOther options:\n"
552 " -h, --help display this help message\n"
553 " -V, --version display version information\n");
554 leak_checker_usage();