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. */
74 const char *dp_desc; /* Datapath description. */
76 /* Related vconns and network devices. */
77 const char *controller_name; /* Controller (if not discovery mode). */
78 struct svec listeners; /* Listen for management connections. */
79 struct svec snoops; /* Listen for controller snooping conns. */
81 /* Failure behavior. */
82 enum fail_mode fail_mode; /* Act as learning switch if no controller? */
83 int max_idle; /* Idle time for flows in fail-open mode. */
84 int probe_interval; /* # seconds idle before sending echo request. */
85 int max_backoff; /* Max # seconds between connection attempts. */
87 /* Packet-in rate-limiting. */
88 int rate_limit; /* Tokens added to bucket per second. */
89 int burst_limit; /* Maximum number token bucket size. */
91 /* Discovery behavior. */
92 const char *accept_controller_re; /* Controller vconns to accept. */
93 bool update_resolv_conf; /* Update /etc/resolv.conf? */
95 /* Spanning tree protocol. */
99 struct svec netflow; /* NetFlow targets. */
102 static void parse_options(int argc, char *argv[], struct ofsettings *);
103 static void usage(void) NO_RETURN;
106 main(int argc, char *argv[])
108 struct unixctl_server *unixctl;
109 struct ofproto *ofproto;
113 struct netflow_options nf_options;
115 proctitle_init(argc, argv);
116 set_program_name(argv[0]);
119 parse_options(argc, argv, &s);
120 signal(SIGPIPE, SIG_IGN);
122 die_if_already_running();
125 /* Start listening for ovs-appctl requests. */
126 error = unixctl_server_create(NULL, &unixctl);
131 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
132 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
134 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
136 ovs_fatal(error, "could not create datapath");
139 /* Add ports to the datapath if requested by the user. */
143 struct netdev *netdev;
145 SVEC_FOR_EACH (i, port, &s.ports) {
146 error = netdev_open_default(port, &netdev);
148 ovs_fatal(error, "failed to open %s as a device", port);
151 error = dpif_port_add(dpif, port, 0, NULL);
153 ovs_fatal(error, "failed to add %s as a port", port);
158 /* Start OpenFlow processing. */
159 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
161 ovs_fatal(error, "could not initialize openflow switch");
163 error = ofproto_set_in_band(ofproto, s.in_band);
165 ovs_fatal(error, "failed to configure in-band control");
167 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
168 s.update_resolv_conf);
170 ovs_fatal(error, "failed to configure controller discovery");
173 ofproto_set_datapath_id(ofproto, s.datapath_id);
175 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
176 s.serial_desc, s.dp_desc);
177 if (!s.listeners.n) {
178 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
179 ovs_rundir, s.dp_name));
180 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
181 svec_clear(&s.listeners);
183 error = ofproto_set_listeners(ofproto, &s.listeners);
185 ovs_fatal(error, "failed to configure management connections");
187 error = ofproto_set_snoops(ofproto, &s.snoops);
190 "failed to configure controller snooping connections");
192 memset(&nf_options, 0, sizeof nf_options);
193 nf_options.collectors = s.netflow;
194 error = ofproto_set_netflow(ofproto, &nf_options);
196 ovs_fatal(error, "failed to configure NetFlow collectors");
198 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
199 ofproto_set_probe_interval(ofproto, s.probe_interval);
200 ofproto_set_max_backoff(ofproto, s.max_backoff);
201 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
202 error = ofproto_set_stp(ofproto, s.enable_stp);
204 ovs_fatal(error, "failed to configure STP");
207 error = ofproto_set_controller(ofproto, s.controller_name);
209 ovs_fatal(error, "failed to configure controller");
213 daemonize_complete();
215 while (ofproto_is_alive(ofproto)) {
216 error = ofproto_run(ofproto);
218 ovs_fatal(error, "unrecoverable datapath error");
220 unixctl_server_run(unixctl);
224 ofproto_wait(ofproto);
225 unixctl_server_wait(unixctl);
236 /* User interface. */
239 parse_options(int argc, char *argv[], struct ofsettings *s)
242 OPT_DATAPATH_ID = UCHAR_MAX + 1,
252 OPT_INACTIVITY_PROBE,
258 OPT_BOOTSTRAP_CA_CERT,
267 LEAK_CHECKER_OPTION_ENUMS
269 static struct option long_options[] = {
270 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
271 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
272 {"hw-desc", required_argument, 0, OPT_HW_DESC},
273 {"sw-desc", required_argument, 0, OPT_SW_DESC},
274 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
275 {"dp-desc", required_argument, 0, OPT_DP_DESC},
276 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
277 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
278 {"config", required_argument, 0, 'F'},
279 {"br-name", required_argument, 0, OPT_BR_NAME},
280 {"fail", required_argument, 0, OPT_FAIL_MODE},
281 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
282 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
283 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
284 {"listen", required_argument, 0, 'l'},
285 {"snoop", required_argument, 0, OPT_SNOOP},
286 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
287 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
288 {"stp", no_argument, 0, OPT_STP},
289 {"no-stp", no_argument, 0, OPT_NO_STP},
290 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
291 {"in-band", no_argument, 0, OPT_IN_BAND},
292 {"netflow", required_argument, 0, OPT_NETFLOW},
293 {"ports", required_argument, 0, OPT_PORTS},
294 {"verbose", optional_argument, 0, 'v'},
295 {"help", no_argument, 0, 'h'},
296 {"version", no_argument, 0, 'V'},
299 LEAK_CHECKER_LONG_OPTIONS,
301 STREAM_SSL_LONG_OPTIONS
302 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
306 char *short_options = long_options_to_short_options(long_options);
308 /* Set defaults that we can figure out before parsing options. */
313 s->serial_desc = NULL;
315 svec_init(&s->listeners);
316 svec_init(&s->snoops);
317 s->fail_mode = FAIL_OPEN;
319 s->probe_interval = 0;
321 s->update_resolv_conf = true;
324 s->accept_controller_re = NULL;
325 s->enable_stp = false;
327 svec_init(&s->netflow);
328 svec_init(&s->ports);
332 c = getopt_long(argc, argv, short_options, long_options, NULL);
338 case OPT_DATAPATH_ID:
339 if (!dpid_from_string(optarg, &s->datapath_id)) {
340 ovs_fatal(0, "argument to --datapath-id must be "
341 "exactly 16 hex digits and may not be all-zero");
346 s->mfr_desc = optarg;
357 case OPT_SERIAL_DESC:
358 s->serial_desc = optarg;
365 case OPT_ACCEPT_VCONN:
366 s->accept_controller_re = optarg;
369 case OPT_NO_RESOLV_CONF:
370 s->update_resolv_conf = false;
374 if (!strcmp(optarg, "open")) {
375 s->fail_mode = FAIL_OPEN;
376 } else if (!strcmp(optarg, "closed")) {
377 s->fail_mode = FAIL_CLOSED;
379 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
383 case OPT_INACTIVITY_PROBE:
384 s->probe_interval = atoi(optarg);
385 if (s->probe_interval < 5) {
386 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
391 if (!strcmp(optarg, "permanent")) {
392 s->max_idle = OFP_FLOW_PERMANENT;
394 s->max_idle = atoi(optarg);
395 if (s->max_idle < 1 || s->max_idle > 65535) {
396 ovs_fatal(0, "--max-idle argument must be between 1 and "
397 "65535 or the word 'permanent'");
402 case OPT_MAX_BACKOFF:
403 s->max_backoff = atoi(optarg);
404 if (s->max_backoff < 1) {
405 ovs_fatal(0, "--max-backoff argument must be at least 1");
406 } else if (s->max_backoff > 3600) {
407 s->max_backoff = 3600;
413 s->rate_limit = atoi(optarg);
414 if (s->rate_limit < 1) {
415 ovs_fatal(0, "--rate-limit argument must be at least 1");
418 s->rate_limit = 1000;
422 case OPT_BURST_LIMIT:
423 s->burst_limit = atoi(optarg);
424 if (s->burst_limit < 1) {
425 ovs_fatal(0, "--burst-limit argument must be at least 1");
430 s->enable_stp = true;
434 s->enable_stp = false;
437 case OPT_OUT_OF_BAND:
446 svec_add(&s->netflow, optarg);
450 svec_add(&s->listeners, optarg);
454 svec_add(&s->snoops, optarg);
458 svec_split(&s->ports, optarg, ",");
465 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
468 DAEMON_OPTION_HANDLERS
472 LEAK_CHECKER_OPTION_HANDLERS
475 STREAM_SSL_OPTION_HANDLERS
477 case OPT_BOOTSTRAP_CA_CERT:
478 stream_ssl_set_ca_cert_file(optarg, true);
493 if (argc < 1 || argc > 2) {
494 ovs_fatal(0, "need one or two non-option arguments; "
495 "use --help for usage");
498 /* Local and remote vconns. */
499 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
501 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
503 /* Set accept_controller_regex. */
504 if (!s->accept_controller_re) {
505 s->accept_controller_re
506 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
509 /* Mode of operation. */
510 s->discovery = s->controller_name == NULL;
511 if (s->discovery && !s->in_band) {
512 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
516 if (s->rate_limit && s->rate_limit < 100) {
517 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
524 printf("%s: an OpenFlow switch implementation.\n"
525 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
526 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
527 "CONTROLLER is an active OpenFlow connection method; if it is\n"
528 "omitted, then ovs-openflowd performs controller discovery.\n",
529 program_name, program_name);
530 vconn_usage(true, true, true);
531 printf("\nOpenFlow options:\n"
532 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
533 " (ID must consist of 16 hex digits)\n"
534 " --mfr-desc=MFR Identify manufacturer as MFR\n"
535 " --hw-desc=HW Identify hardware as HW\n"
536 " --sw-desc=SW Identify software as SW\n"
537 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
538 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
539 "\nController discovery options:\n"
540 " --accept-vconn=REGEX accept matching discovered controllers\n"
541 " --no-resolv-conf do not update /etc/resolv.conf\n"
542 "\nNetworking options:\n"
543 " --fail=open|closed when controller connection fails:\n"
544 " closed: drop all packets\n"
545 " open (default): act as learning switch\n"
546 " --inactivity-probe=SECS time between inactivity probes\n"
547 " --max-idle=SECS max idle for flows set up by switch\n"
548 " --max-backoff=SECS max time between controller connection\n"
549 " attempts (default: 8 seconds)\n"
550 " -l, --listen=METHOD allow management connections on METHOD\n"
551 " (a passive OpenFlow connection method)\n"
552 " --snoop=METHOD allow controller snooping on METHOD\n"
553 " (a passive OpenFlow connection method)\n"
554 " --out-of-band controller connection is out-of-band\n"
555 " --netflow=HOST:PORT configure NetFlow output target\n"
556 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
557 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
558 " --burst-limit=BURST limit on packet credit for idle time\n");
561 printf("\nOther options:\n"
562 " -h, --help display this help message\n"
563 " -V, --version display version information\n");
564 leak_checker_usage();