2 * Copyright (c) 2008, 2009, 2010, 2011 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"
48 VLOG_DEFINE_THIS_MODULE(openflowd);
50 /* Settings that may be configured by the user. */
52 const char *unixctl_path; /* File name for unixctl socket. */
54 /* Controller configuration. */
55 struct ofproto_controller *controllers;
57 enum ofproto_fail_mode fail_mode;
58 bool run_forever; /* Continue running even with no controller? */
61 uint64_t datapath_id; /* Datapath ID. */
62 char *dp_name; /* Name of local datapath. */
63 char *dp_type; /* Type of local datapath. */
64 struct sset ports; /* Set of ports to add to datapath (if any). */
66 /* Description strings. */
67 const char *mfr_desc; /* Manufacturer. */
68 const char *hw_desc; /* Hardware. */
69 const char *sw_desc; /* Software version. */
70 const char *serial_desc; /* Serial number. */
71 const char *dp_desc; /* Datapath description. */
73 /* Related vconns and network devices. */
74 struct sset snoops; /* Listen for controller snooping conns. */
76 /* Failure behavior. */
77 int max_idle; /* Idle time for flows in fail-open mode. */
80 struct sset netflow; /* NetFlow targets. */
83 static unixctl_cb_func test_openflowd_exit;
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;
95 struct netflow_options nf_options;
99 proctitle_init(argc, argv);
100 set_program_name(argv[0]);
101 parse_options(argc, argv, &s);
102 signal(SIGPIPE, SIG_IGN);
106 /* Start listening for ovs-appctl requests. */
107 error = unixctl_server_create(s.unixctl_path, &unixctl);
112 unixctl_command_register("exit", "", test_openflowd_exit, &exiting);
114 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
115 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
117 error = ofproto_create(s.dp_name, s.dp_type, &ofproto);
119 VLOG_FATAL("could not initialize OpenFlow switch (%s)",
123 /* Add ports to the datapath if requested by the user. */
124 SSET_FOR_EACH (port, &s.ports) {
125 struct netdev *netdev;
128 netdev_parse_name(port, &name, &type);
129 error = netdev_open(name, type, &netdev);
131 VLOG_FATAL("%s: failed to open network device (%s)",
132 port, strerror(error));
137 error = ofproto_port_add(ofproto, netdev, NULL);
139 VLOG_FATAL("failed to add %s as a port (%s)",
140 port, strerror(error));
143 netdev_close(netdev);
146 /* Configure OpenFlow switch. */
148 ofproto_set_datapath_id(ofproto, s.datapath_id);
150 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
151 s.serial_desc, s.dp_desc);
152 error = ofproto_set_snoops(ofproto, &s.snoops);
154 VLOG_FATAL("failed to configure controller snooping connections (%s)",
157 memset(&nf_options, 0, sizeof nf_options);
158 nf_options.collectors = s.netflow;
159 error = ofproto_set_netflow(ofproto, &nf_options);
161 VLOG_FATAL("failed to configure NetFlow collectors (%s)",
164 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
165 ofproto_set_fail_mode(ofproto, s.fail_mode);
167 daemonize_complete();
170 while (!exiting && (s.run_forever || ofproto_is_alive(ofproto))) {
171 error = ofproto_run(ofproto);
173 VLOG_FATAL("unrecoverable datapath error (%s)", strerror(error));
175 unixctl_server_run(unixctl);
178 ofproto_wait(ofproto);
179 unixctl_server_wait(unixctl);
182 poll_immediate_wake();
187 ofproto_destroy(ofproto);
193 test_openflowd_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
196 bool *exiting = exiting_;
198 unixctl_command_reply(conn, 200, NULL);
201 /* User interface. */
203 /* Breaks 'ports' apart at commas and adds each resulting word to 'ports'. */
205 parse_ports(const char *s_, struct sset *ports)
207 char *s = xstrdup(s_);
208 char *save_ptr = NULL;
211 for (token = strtok_r(s, ",", &save_ptr); token != NULL;
212 token = strtok_r(NULL, ",", &save_ptr)) {
213 sset_add(ports, token);
219 parse_options(int argc, char *argv[], struct ofsettings *s)
222 OPT_DATAPATH_ID = UCHAR_MAX + 1,
230 OPT_INACTIVITY_PROBE,
236 OPT_BOOTSTRAP_CA_CERT,
244 LEAK_CHECKER_OPTION_ENUMS,
247 static struct option long_options[] = {
248 {"datapath-id", required_argument, NULL, OPT_DATAPATH_ID},
249 {"mfr-desc", required_argument, NULL, OPT_MFR_DESC},
250 {"hw-desc", required_argument, NULL, OPT_HW_DESC},
251 {"sw-desc", required_argument, NULL, OPT_SW_DESC},
252 {"serial-desc", required_argument, NULL, OPT_SERIAL_DESC},
253 {"dp-desc", required_argument, NULL, OPT_DP_DESC},
254 {"config", required_argument, NULL, 'F'},
255 {"br-name", required_argument, NULL, OPT_BR_NAME},
256 {"fail", required_argument, NULL, OPT_FAIL_MODE},
257 {"inactivity-probe", required_argument, NULL, OPT_INACTIVITY_PROBE},
258 {"max-idle", required_argument, NULL, OPT_MAX_IDLE},
259 {"max-backoff", required_argument, NULL, OPT_MAX_BACKOFF},
260 {"listen", required_argument, NULL, 'l'},
261 {"snoop", required_argument, NULL, OPT_SNOOP},
262 {"rate-limit", optional_argument, NULL, OPT_RATE_LIMIT},
263 {"burst-limit", required_argument, NULL, OPT_BURST_LIMIT},
264 {"out-of-band", no_argument, NULL, OPT_OUT_OF_BAND},
265 {"in-band", no_argument, NULL, OPT_IN_BAND},
266 {"netflow", required_argument, NULL, OPT_NETFLOW},
267 {"ports", required_argument, NULL, OPT_PORTS},
268 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
269 {"enable-dummy", no_argument, NULL, OPT_ENABLE_DUMMY},
270 {"verbose", optional_argument, NULL, 'v'},
271 {"help", no_argument, NULL, 'h'},
272 {"version", no_argument, NULL, 'V'},
275 LEAK_CHECKER_LONG_OPTIONS,
276 STREAM_SSL_LONG_OPTIONS,
277 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
280 char *short_options = long_options_to_short_options(long_options);
281 struct ofproto_controller controller_opts;
282 struct sset controllers;
286 /* Set defaults that we can figure out before parsing options. */
287 controller_opts.target = NULL;
288 controller_opts.max_backoff = 8;
289 controller_opts.probe_interval = 5;
290 controller_opts.band = OFPROTO_IN_BAND;
291 controller_opts.rate_limit = 0;
292 controller_opts.burst_limit = 0;
293 s->unixctl_path = NULL;
294 s->fail_mode = OFPROTO_FAIL_STANDALONE;
299 s->serial_desc = NULL;
301 sset_init(&controllers);
302 sset_init(&s->snoops);
304 sset_init(&s->netflow);
305 sset_init(&s->ports);
309 c = getopt_long(argc, argv, short_options, long_options, NULL);
315 case OPT_DATAPATH_ID:
316 if (!dpid_from_string(optarg, &s->datapath_id)) {
317 VLOG_FATAL("argument to --datapath-id must be exactly 16 hex "
318 "digits and may not be all-zero");
323 s->mfr_desc = optarg;
334 case OPT_SERIAL_DESC:
335 s->serial_desc = optarg;
343 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
344 s->fail_mode = OFPROTO_FAIL_STANDALONE;
345 } else if (!strcmp(optarg, "closed")
346 || !strcmp(optarg, "secure")) {
347 s->fail_mode = OFPROTO_FAIL_SECURE;
349 VLOG_FATAL("--fail argument must be \"standalone\" "
354 case OPT_INACTIVITY_PROBE:
355 controller_opts.probe_interval = atoi(optarg);
356 if (controller_opts.probe_interval < 5) {
357 VLOG_FATAL("--inactivity-probe argument must be at least 5");
362 if (!strcmp(optarg, "permanent")) {
363 s->max_idle = OFP_FLOW_PERMANENT;
365 s->max_idle = atoi(optarg);
366 if (s->max_idle < 1 || s->max_idle > 65535) {
367 VLOG_FATAL("--max-idle argument must be between 1 and "
368 "65535 or the word 'permanent'");
373 case OPT_MAX_BACKOFF:
374 controller_opts.max_backoff = atoi(optarg);
375 if (controller_opts.max_backoff < 1) {
376 VLOG_FATAL("--max-backoff argument must be at least 1");
377 } else if (controller_opts.max_backoff > 3600) {
378 controller_opts.max_backoff = 3600;
384 controller_opts.rate_limit = atoi(optarg);
385 if (controller_opts.rate_limit < 1) {
386 VLOG_FATAL("--rate-limit argument must be at least 1");
389 controller_opts.rate_limit = 1000;
393 case OPT_BURST_LIMIT:
394 controller_opts.burst_limit = atoi(optarg);
395 if (controller_opts.burst_limit < 1) {
396 VLOG_FATAL("--burst-limit argument must be at least 1");
400 case OPT_OUT_OF_BAND:
401 controller_opts.band = OFPROTO_OUT_OF_BAND;
405 controller_opts.band = OFPROTO_IN_BAND;
409 sset_add(&s->netflow, optarg);
413 sset_add(&controllers, optarg);
417 sset_add(&s->snoops, optarg);
421 parse_ports(optarg, &s->ports);
425 s->unixctl_path = optarg;
428 case OPT_ENABLE_DUMMY:
436 ovs_print_version(OFP_VERSION, OFP_VERSION);
439 DAEMON_OPTION_HANDLERS
443 LEAK_CHECKER_OPTION_HANDLERS
445 STREAM_SSL_OPTION_HANDLERS
447 case OPT_BOOTSTRAP_CA_CERT:
448 stream_ssl_set_ca_cert_file(optarg, true);
463 VLOG_FATAL("need at least two non-option arguments; "
464 "use --help for usage");
468 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
469 VLOG_WARN("Rate limit set to unusually low value %d",
470 controller_opts.rate_limit);
474 ofproto_parse_name(argv[0], &s->dp_name, &s->dp_type);
476 /* Figure out controller names. */
477 s->run_forever = false;
478 if (sset_is_empty(&controllers)) {
479 sset_add_and_free(&controllers, xasprintf("punix:%s/%s.mgmt",
480 ovs_rundir(), s->dp_name));
482 for (i = 1; i < argc; i++) {
483 if (!strcmp(argv[i], "none")) {
484 s->run_forever = true;
486 sset_add(&controllers, argv[i]);
490 /* Set up controllers. */
491 s->n_controllers = sset_count(&controllers);
492 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
494 SSET_FOR_EACH (name, &controllers) {
495 s->controllers[i] = controller_opts;
496 s->controllers[i].target = xstrdup(name);
499 sset_destroy(&controllers);
505 printf("%s: an OpenFlow switch implementation.\n"
506 "usage: %s [OPTIONS] [TYPE@]DATAPATH CONTROLLER...\n"
507 "where DATAPATH is a local datapath (e.g. \"dp0\")\n"
508 "optionally with an explicit TYPE (default: \"system\").\n"
509 "Each CONTROLLER is an active OpenFlow connection method.\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 "\nNetworking options:\n"
521 " --fail=open|closed when controller connection fails:\n"
522 " closed: drop all packets\n"
523 " open (default): act as learning switch\n"
524 " --inactivity-probe=SECS time between inactivity probes\n"
525 " --max-idle=SECS max idle for flows set up by switch\n"
526 " --max-backoff=SECS max time between controller connection\n"
527 " attempts (default: 8 seconds)\n"
528 " -l, --listen=METHOD allow management connections on METHOD\n"
529 " (a passive OpenFlow connection method)\n"
530 " --snoop=METHOD allow controller snooping on METHOD\n"
531 " (a passive OpenFlow connection method)\n"
532 " --out-of-band controller connection is out-of-band\n"
533 " --netflow=HOST:PORT configure NetFlow output target\n"
534 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
535 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
536 " --burst-limit=BURST limit on packet credit for idle time\n");
539 printf("\nOther options:\n"
540 " --unixctl=SOCKET override default control socket name\n"
541 " -h, --help display this help message\n"
542 " -V, --version display version information\n");
543 leak_checker_usage();