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 const char *dp_name; /* Name of local datapath. */
66 struct svec ports; /* Set of ports to add to datapath (if any). */
68 /* Description strings. */
69 const char *mfr_desc; /* Manufacturer. */
70 const char *hw_desc; /* Hardware. */
71 const char *sw_desc; /* Software version. */
72 const char *serial_desc; /* Serial number. */
74 /* Related vconns and network devices. */
75 const char *controller_name; /* Controller (if not discovery mode). */
76 struct svec listeners; /* Listen for management connections. */
77 struct svec snoops; /* Listen for controller snooping conns. */
79 /* Failure behavior. */
80 enum fail_mode fail_mode; /* Act as learning switch if no controller? */
81 int max_idle; /* Idle time for flows in fail-open mode. */
82 int probe_interval; /* # seconds idle before sending echo request. */
83 int max_backoff; /* Max # seconds between connection attempts. */
85 /* Packet-in rate-limiting. */
86 int rate_limit; /* Tokens added to bucket per second. */
87 int burst_limit; /* Maximum number token bucket size. */
89 /* Discovery behavior. */
90 const char *accept_controller_re; /* Controller vconns to accept. */
91 bool update_resolv_conf; /* Update /etc/resolv.conf? */
93 /* Spanning tree protocol. */
97 uint64_t mgmt_id; /* Management ID. */
100 struct svec netflow; /* NetFlow targets. */
103 static void parse_options(int argc, char *argv[], struct ofsettings *);
104 static void usage(void) NO_RETURN;
107 main(int argc, char *argv[])
109 struct unixctl_server *unixctl;
110 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 /* Create the datapath and add ports to it, if requested by the user. */
140 error = dpif_create_and_open(s.dp_name, &dpif);
142 ovs_fatal(error, "could not create datapath");
145 SVEC_FOR_EACH (i, port, &s.ports) {
146 error = dpif_port_add(dpif, port, 0, NULL);
148 ovs_fatal(error, "failed to add %s as a port", port);
154 /* Start OpenFlow processing. */
155 error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
157 ovs_fatal(error, "could not initialize openflow switch");
159 error = ofproto_set_in_band(ofproto, s.in_band);
161 ovs_fatal(error, "failed to configure in-band control");
163 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
164 s.update_resolv_conf);
166 ovs_fatal(error, "failed to configure controller discovery");
169 ofproto_set_datapath_id(ofproto, s.datapath_id);
172 ofproto_set_mgmt_id(ofproto, s.mgmt_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);
232 /* User interface. */
235 parse_options(int argc, char *argv[], struct ofsettings *s)
238 OPT_DATAPATH_ID = UCHAR_MAX + 1,
247 OPT_INACTIVITY_PROBE,
253 OPT_BOOTSTRAP_CA_CERT,
262 LEAK_CHECKER_OPTION_ENUMS
264 static struct option long_options[] = {
265 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
266 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
267 {"hardware", required_argument, 0, OPT_HARDWARE},
268 {"software", required_argument, 0, OPT_SOFTWARE},
269 {"serial", required_argument, 0, OPT_SERIAL},
270 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
271 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
272 {"config", required_argument, 0, 'F'},
273 {"br-name", required_argument, 0, OPT_BR_NAME},
274 {"fail", required_argument, 0, OPT_FAIL_MODE},
275 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
276 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
277 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
278 {"listen", required_argument, 0, 'l'},
279 {"snoop", required_argument, 0, OPT_SNOOP},
280 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
281 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
282 {"stp", no_argument, 0, OPT_STP},
283 {"no-stp", no_argument, 0, OPT_NO_STP},
284 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
285 {"in-band", no_argument, 0, OPT_IN_BAND},
286 {"netflow", required_argument, 0, OPT_NETFLOW},
287 {"mgmt-id", required_argument, 0, OPT_MGMT_ID},
288 {"ports", required_argument, 0, OPT_PORTS},
289 {"verbose", optional_argument, 0, 'v'},
290 {"help", no_argument, 0, 'h'},
291 {"version", no_argument, 0, 'V'},
294 LEAK_CHECKER_LONG_OPTIONS,
296 STREAM_SSL_LONG_OPTIONS
297 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
301 char *short_options = long_options_to_short_options(long_options);
303 /* Set defaults that we can figure out before parsing options. */
308 s->serial_desc = NULL;
309 svec_init(&s->listeners);
310 svec_init(&s->snoops);
311 s->fail_mode = FAIL_OPEN;
313 s->probe_interval = 0;
315 s->update_resolv_conf = true;
318 s->accept_controller_re = NULL;
319 s->enable_stp = false;
321 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 if (strlen(optarg) != 12
442 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
443 ovs_fatal(0, "argument to --mgmt-id must be "
444 "exactly 12 hex digits");
446 s->mgmt_id = strtoll(optarg, NULL, 16);
448 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
453 svec_add(&s->listeners, optarg);
457 svec_add(&s->snoops, optarg);
461 svec_split(&s->ports, optarg, ",");
468 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
471 DAEMON_OPTION_HANDLERS
475 LEAK_CHECKER_OPTION_HANDLERS
478 STREAM_SSL_OPTION_HANDLERS
480 case OPT_BOOTSTRAP_CA_CERT:
481 stream_ssl_set_ca_cert_file(optarg, true);
496 if (argc < 1 || argc > 2) {
497 ovs_fatal(0, "need one or two non-option arguments; "
498 "use --help for usage");
501 /* Local and remote vconns. */
502 s->dp_name = argv[0];
503 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
505 /* Set accept_controller_regex. */
506 if (!s->accept_controller_re) {
507 s->accept_controller_re
508 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
511 /* Mode of operation. */
512 s->discovery = s->controller_name == NULL;
513 if (s->discovery && !s->in_band) {
514 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
518 if (s->rate_limit && s->rate_limit < 100) {
519 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
526 printf("%s: an OpenFlow switch implementation.\n"
527 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
528 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
529 "CONTROLLER is an active OpenFlow connection method; if it is\n"
530 "omitted, then ovs-openflowd performs controller discovery.\n",
531 program_name, program_name);
532 vconn_usage(true, true, true);
533 printf("\nOpenFlow options:\n"
534 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
535 " (ID must consist of 12 hex digits)\n"
536 " --mgmt-id=ID Use ID as the management ID\n"
537 " (ID must consist of 12 hex digits)\n"
538 " --manufacturer=MFR Identify manufacturer as MFR\n"
539 " --hardware=HW Identify hardware as HW\n"
540 " --software=SW Identify software as SW\n"
541 " --serial=SERIAL Identify serial number as SERIAL\n"
542 "\nController discovery options:\n"
543 " --accept-vconn=REGEX accept matching discovered controllers\n"
544 " --no-resolv-conf do not update /etc/resolv.conf\n"
545 "\nNetworking options:\n"
546 " --fail=open|closed when controller connection fails:\n"
547 " closed: drop all packets\n"
548 " open (default): act as learning switch\n"
549 " --inactivity-probe=SECS time between inactivity probes\n"
550 " --max-idle=SECS max idle for flows set up by switch\n"
551 " --max-backoff=SECS max time between controller connection\n"
552 " attempts (default: 8 seconds)\n"
553 " -l, --listen=METHOD allow management connections on METHOD\n"
554 " (a passive OpenFlow connection method)\n"
555 " --snoop=METHOD allow controller snooping on METHOD\n"
556 " (a passive OpenFlow connection method)\n"
557 " --out-of-band controller connection is out-of-band\n"
558 " --netflow=HOST:PORT configure NetFlow output target\n"
559 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
560 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
561 " --burst-limit=BURST limit on packet credit for idle time\n");
564 printf("\nOther options:\n"
565 " -h, --help display this help message\n"
566 " -V, --version display version information\n");
567 leak_checker_usage();