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 VLOG_DEFINE_THIS_MODULE(openflowd)
51 /* Settings that may be configured by the user. */
53 /* Controller configuration. */
54 struct ofproto_controller *controllers;
56 enum ofproto_fail_mode fail_mode;
59 uint64_t datapath_id; /* Datapath ID. */
60 char *dp_name; /* Name of local datapath. */
61 char *dp_type; /* Type of local datapath. */
62 struct svec ports; /* Set of ports to add to datapath (if any). */
64 /* Description strings. */
65 const char *mfr_desc; /* Manufacturer. */
66 const char *hw_desc; /* Hardware. */
67 const char *sw_desc; /* Software version. */
68 const char *serial_desc; /* Serial number. */
69 const char *dp_desc; /* Datapath description. */
71 /* Related vconns and network devices. */
72 struct svec listeners; /* Listen for management connections. */
73 struct svec snoops; /* Listen for controller snooping conns. */
75 /* Failure behavior. */
76 int max_idle; /* Idle time for flows in fail-open mode. */
78 /* Spanning tree protocol. */
82 struct svec netflow; /* NetFlow targets. */
85 static void parse_options(int argc, char *argv[], struct ofsettings *);
86 static void usage(void) NO_RETURN;
89 main(int argc, char *argv[])
91 struct unixctl_server *unixctl;
92 struct ofproto *ofproto;
96 struct netflow_options nf_options;
98 proctitle_init(argc, argv);
99 set_program_name(argv[0]);
100 parse_options(argc, argv, &s);
101 signal(SIGPIPE, SIG_IGN);
103 die_if_already_running();
106 /* Start listening for ovs-appctl requests. */
107 error = unixctl_server_create(NULL, &unixctl);
112 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
113 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
115 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
117 ovs_fatal(error, "could not create datapath");
120 /* Add ports to the datapath if requested by the user. */
125 SVEC_FOR_EACH (i, port, &s.ports) {
126 error = dpif_port_add(dpif, port, 0, NULL);
128 ovs_fatal(error, "failed to add %s as a port", port);
133 /* Start OpenFlow processing. */
134 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
136 ovs_fatal(error, "could not initialize openflow switch");
139 ofproto_set_datapath_id(ofproto, s.datapath_id);
141 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
142 s.serial_desc, s.dp_desc);
143 if (!s.listeners.n) {
144 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
145 ovs_rundir, s.dp_name));
146 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
147 svec_clear(&s.listeners);
149 error = ofproto_set_listeners(ofproto, &s.listeners);
151 ovs_fatal(error, "failed to configure management connections");
153 error = ofproto_set_snoops(ofproto, &s.snoops);
156 "failed to configure controller snooping connections");
158 memset(&nf_options, 0, sizeof nf_options);
159 nf_options.collectors = s.netflow;
160 error = ofproto_set_netflow(ofproto, &nf_options);
162 ovs_fatal(error, "failed to configure NetFlow collectors");
164 error = ofproto_set_stp(ofproto, s.enable_stp);
166 ovs_fatal(error, "failed to configure STP");
168 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
169 ofproto_set_fail_mode(ofproto, s.fail_mode);
171 daemonize_complete();
173 while (ofproto_is_alive(ofproto)) {
174 error = ofproto_run(ofproto);
176 ovs_fatal(error, "unrecoverable datapath error");
178 unixctl_server_run(unixctl);
182 ofproto_wait(ofproto);
183 unixctl_server_wait(unixctl);
194 /* User interface. */
197 parse_options(int argc, char *argv[], struct ofsettings *s)
200 OPT_DATAPATH_ID = UCHAR_MAX + 1,
210 OPT_INACTIVITY_PROBE,
216 OPT_BOOTSTRAP_CA_CERT,
224 LEAK_CHECKER_OPTION_ENUMS
226 static struct option long_options[] = {
227 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
228 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
229 {"hw-desc", required_argument, 0, OPT_HW_DESC},
230 {"sw-desc", required_argument, 0, OPT_SW_DESC},
231 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
232 {"dp-desc", required_argument, 0, OPT_DP_DESC},
233 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
234 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
235 {"config", required_argument, 0, 'F'},
236 {"br-name", required_argument, 0, OPT_BR_NAME},
237 {"fail", required_argument, 0, OPT_FAIL_MODE},
238 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
239 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
240 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
241 {"listen", required_argument, 0, 'l'},
242 {"snoop", required_argument, 0, OPT_SNOOP},
243 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
244 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
245 {"stp", no_argument, 0, OPT_STP},
246 {"no-stp", no_argument, 0, OPT_NO_STP},
247 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
248 {"in-band", no_argument, 0, OPT_IN_BAND},
249 {"netflow", required_argument, 0, OPT_NETFLOW},
250 {"ports", required_argument, 0, OPT_PORTS},
251 {"verbose", optional_argument, 0, 'v'},
252 {"help", no_argument, 0, 'h'},
253 {"version", no_argument, 0, 'V'},
256 LEAK_CHECKER_LONG_OPTIONS,
258 STREAM_SSL_LONG_OPTIONS
259 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
263 char *short_options = long_options_to_short_options(long_options);
264 struct ofproto_controller controller_opts;
266 /* Set defaults that we can figure out before parsing options. */
267 controller_opts.target = NULL;
268 controller_opts.max_backoff = 8;
269 controller_opts.probe_interval = 5;
270 controller_opts.band = OFPROTO_IN_BAND;
271 controller_opts.accept_re = NULL;
272 controller_opts.update_resolv_conf = true;
273 controller_opts.rate_limit = 0;
274 controller_opts.burst_limit = 0;
275 s->fail_mode = OFPROTO_FAIL_STANDALONE;
280 s->serial_desc = NULL;
282 svec_init(&s->listeners);
283 svec_init(&s->snoops);
285 s->enable_stp = false;
286 svec_init(&s->netflow);
287 svec_init(&s->ports);
291 c = getopt_long(argc, argv, short_options, long_options, NULL);
297 case OPT_DATAPATH_ID:
298 if (!dpid_from_string(optarg, &s->datapath_id)) {
299 ovs_fatal(0, "argument to --datapath-id must be "
300 "exactly 16 hex digits and may not be all-zero");
305 s->mfr_desc = optarg;
316 case OPT_SERIAL_DESC:
317 s->serial_desc = optarg;
324 case OPT_ACCEPT_VCONN:
325 controller_opts.accept_re = optarg;
328 case OPT_NO_RESOLV_CONF:
329 controller_opts.update_resolv_conf = false;
333 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
334 s->fail_mode = OFPROTO_FAIL_STANDALONE;
335 } else if (!strcmp(optarg, "closed")
336 || !strcmp(optarg, "secure")) {
337 s->fail_mode = OFPROTO_FAIL_SECURE;
339 ovs_fatal(0, "--fail argument must be \"standalone\" "
344 case OPT_INACTIVITY_PROBE:
345 controller_opts.probe_interval = atoi(optarg);
346 if (controller_opts.probe_interval < 5) {
347 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
352 if (!strcmp(optarg, "permanent")) {
353 s->max_idle = OFP_FLOW_PERMANENT;
355 s->max_idle = atoi(optarg);
356 if (s->max_idle < 1 || s->max_idle > 65535) {
357 ovs_fatal(0, "--max-idle argument must be between 1 and "
358 "65535 or the word 'permanent'");
363 case OPT_MAX_BACKOFF:
364 controller_opts.max_backoff = atoi(optarg);
365 if (controller_opts.max_backoff < 1) {
366 ovs_fatal(0, "--max-backoff argument must be at least 1");
367 } else if (controller_opts.max_backoff > 3600) {
368 controller_opts.max_backoff = 3600;
374 controller_opts.rate_limit = atoi(optarg);
375 if (controller_opts.rate_limit < 1) {
376 ovs_fatal(0, "--rate-limit argument must be at least 1");
379 controller_opts.rate_limit = 1000;
383 case OPT_BURST_LIMIT:
384 controller_opts.burst_limit = atoi(optarg);
385 if (controller_opts.burst_limit < 1) {
386 ovs_fatal(0, "--burst-limit argument must be at least 1");
391 s->enable_stp = true;
395 s->enable_stp = false;
398 case OPT_OUT_OF_BAND:
399 controller_opts.band = OFPROTO_OUT_OF_BAND;
403 controller_opts.band = OFPROTO_IN_BAND;
407 svec_add(&s->netflow, optarg);
411 svec_add(&s->listeners, optarg);
415 svec_add(&s->snoops, optarg);
419 svec_split(&s->ports, optarg, ",");
426 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
429 DAEMON_OPTION_HANDLERS
433 LEAK_CHECKER_OPTION_HANDLERS
436 STREAM_SSL_OPTION_HANDLERS
438 case OPT_BOOTSTRAP_CA_CERT:
439 stream_ssl_set_ca_cert_file(optarg, true);
455 ovs_fatal(0, "need at least one non-option arguments; "
456 "use --help for usage");
459 /* Set accept_controller_regex. */
460 if (!controller_opts.accept_re) {
461 controller_opts.accept_re
462 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
466 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
467 VLOG_WARN("Rate limit set to unusually low value %d",
468 controller_opts.rate_limit);
472 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
475 s->n_controllers = argc > 1 ? argc - 1 : 1;
476 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
480 for (i = 0; i < s->n_controllers; i++) {
481 s->controllers[i] = controller_opts;
482 s->controllers[i].target = argv[i + 1];
485 s->controllers[0] = controller_opts;
486 s->controllers[0].target = "discover";
490 if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
493 for (i = 0; i < s->n_controllers; i++) {
494 if (!strcmp(s->controllers[i].target, "discover")) {
495 ovs_fatal(0, "Cannot perform discovery with out-of-band "
505 printf("%s: an OpenFlow switch implementation.\n"
506 "usage: %s [OPTIONS] DATAPATH [CONTROLLER...]\n"
507 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
508 "Each CONTROLLER is an active OpenFlow connection method. If\n"
509 "none is given, ovs-openflowd performs controller discovery.\n",
510 program_name, program_name);
511 vconn_usage(true, true, true);
512 printf("\nOpenFlow options:\n"
513 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
514 " (ID must consist of 16 hex digits)\n"
515 " --mfr-desc=MFR Identify manufacturer as MFR\n"
516 " --hw-desc=HW Identify hardware as HW\n"
517 " --sw-desc=SW Identify software as SW\n"
518 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
519 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
520 "\nController discovery options:\n"
521 " --accept-vconn=REGEX accept matching discovered controllers\n"
522 " --no-resolv-conf do not update /etc/resolv.conf\n"
523 "\nNetworking options:\n"
524 " --fail=open|closed when controller connection fails:\n"
525 " closed: drop all packets\n"
526 " open (default): act as learning switch\n"
527 " --inactivity-probe=SECS time between inactivity probes\n"
528 " --max-idle=SECS max idle for flows set up by switch\n"
529 " --max-backoff=SECS max time between controller connection\n"
530 " attempts (default: 8 seconds)\n"
531 " -l, --listen=METHOD allow management connections on METHOD\n"
532 " (a passive OpenFlow connection method)\n"
533 " --snoop=METHOD allow controller snooping on METHOD\n"
534 " (a passive OpenFlow connection method)\n"
535 " --out-of-band controller connection is out-of-band\n"
536 " --netflow=HOST:PORT configure NetFlow output target\n"
537 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
538 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
539 " --burst-limit=BURST limit on packet credit for idle time\n");
542 printf("\nOther options:\n"
543 " -h, --help display this help message\n"
544 " -V, --version display version information\n");
545 leak_checker_usage();