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"
33 #include "leak-checker.h"
37 #include "ofproto/ofproto.h"
38 #include "openflow/openflow.h"
40 #include "poll-loop.h"
42 #include "stream-ssl.h"
49 VLOG_DEFINE_THIS_MODULE(openflowd);
51 /* Settings that may be configured by the user. */
53 const char *unixctl_path; /* File name for unixctl socket. */
55 /* Controller configuration. */
56 struct ofproto_controller *controllers;
58 enum ofproto_fail_mode fail_mode;
59 bool run_forever; /* Continue running even with no controller? */
62 uint64_t datapath_id; /* Datapath ID. */
63 char *dp_name; /* Name of local datapath. */
64 char *dp_type; /* Type of local datapath. */
65 struct sset ports; /* Set of ports to add to datapath (if any). */
67 /* Description strings. */
68 const char *mfr_desc; /* Manufacturer. */
69 const char *hw_desc; /* Hardware. */
70 const char *sw_desc; /* Software version. */
71 const char *serial_desc; /* Serial number. */
72 const char *dp_desc; /* Datapath description. */
74 /* Related vconns and network devices. */
75 struct sset snoops; /* Listen for controller snooping conns. */
77 /* Failure behavior. */
78 int max_idle; /* Idle time for flows in fail-open mode. */
81 struct sset netflow; /* NetFlow targets. */
84 static unixctl_cb_func ovs_openflowd_exit;
86 static void parse_options(int argc, char *argv[], struct ofsettings *);
87 static void usage(void) NO_RETURN;
90 main(int argc, char *argv[])
92 struct unixctl_server *unixctl;
93 struct ofproto *ofproto;
97 struct netflow_options nf_options;
101 proctitle_init(argc, argv);
102 set_program_name(argv[0]);
103 parse_options(argc, argv, &s);
104 signal(SIGPIPE, SIG_IGN);
108 /* Start listening for ovs-appctl requests. */
109 error = unixctl_server_create(s.unixctl_path, &unixctl);
114 unixctl_command_register("exit", ovs_openflowd_exit, &exiting);
116 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
117 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
119 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
121 VLOG_FATAL("could not create datapath (%s)", strerror(error));
124 /* Add ports to the datapath if requested by the user. */
125 SSET_FOR_EACH (port, &s.ports) {
126 struct netdev *netdev;
128 error = netdev_open_default(port, &netdev);
130 VLOG_FATAL("%s: failed to open network device (%s)",
131 port, strerror(error));
134 error = dpif_port_add(dpif, netdev, NULL);
136 VLOG_FATAL("failed to add %s as a port (%s)",
137 port, strerror(error));
140 netdev_close(netdev);
143 /* Start OpenFlow processing. */
144 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
146 VLOG_FATAL("could not initialize openflow switch (%s)",
150 ofproto_set_datapath_id(ofproto, s.datapath_id);
152 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
153 s.serial_desc, s.dp_desc);
154 error = ofproto_set_snoops(ofproto, &s.snoops);
156 VLOG_FATAL("failed to configure controller snooping connections (%s)",
159 memset(&nf_options, 0, sizeof nf_options);
160 nf_options.collectors = s.netflow;
161 error = ofproto_set_netflow(ofproto, &nf_options);
163 VLOG_FATAL("failed to configure NetFlow collectors (%s)",
166 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
167 ofproto_set_fail_mode(ofproto, s.fail_mode);
169 daemonize_complete();
172 while (!exiting && (s.run_forever || ofproto_is_alive(ofproto))) {
173 error = ofproto_run(ofproto);
175 VLOG_FATAL("unrecoverable datapath error (%s)", strerror(error));
177 unixctl_server_run(unixctl);
181 ofproto_wait(ofproto);
182 unixctl_server_wait(unixctl);
186 poll_immediate_wake();
197 ovs_openflowd_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
200 bool *exiting = exiting_;
202 unixctl_command_reply(conn, 200, NULL);
205 /* User interface. */
207 /* Breaks 'ports' apart at commas and adds each resulting word to 'ports'. */
209 parse_ports(const char *s_, struct sset *ports)
211 char *s = xstrdup(s_);
212 char *save_ptr = NULL;
215 for (token = strtok_r(s, ",", &save_ptr); token != NULL;
216 token = strtok_r(NULL, ",", &save_ptr)) {
217 sset_add(ports, token);
223 parse_options(int argc, char *argv[], struct ofsettings *s)
226 OPT_DATAPATH_ID = UCHAR_MAX + 1,
234 OPT_INACTIVITY_PROBE,
240 OPT_BOOTSTRAP_CA_CERT,
248 LEAK_CHECKER_OPTION_ENUMS,
251 static struct option long_options[] = {
252 {"datapath-id", required_argument, NULL, OPT_DATAPATH_ID},
253 {"mfr-desc", required_argument, NULL, OPT_MFR_DESC},
254 {"hw-desc", required_argument, NULL, OPT_HW_DESC},
255 {"sw-desc", required_argument, NULL, OPT_SW_DESC},
256 {"serial-desc", required_argument, NULL, OPT_SERIAL_DESC},
257 {"dp-desc", required_argument, NULL, OPT_DP_DESC},
258 {"config", required_argument, NULL, 'F'},
259 {"br-name", required_argument, NULL, OPT_BR_NAME},
260 {"fail", required_argument, NULL, OPT_FAIL_MODE},
261 {"inactivity-probe", required_argument, NULL, OPT_INACTIVITY_PROBE},
262 {"max-idle", required_argument, NULL, OPT_MAX_IDLE},
263 {"max-backoff", required_argument, NULL, OPT_MAX_BACKOFF},
264 {"listen", required_argument, NULL, 'l'},
265 {"snoop", required_argument, NULL, OPT_SNOOP},
266 {"rate-limit", optional_argument, NULL, OPT_RATE_LIMIT},
267 {"burst-limit", required_argument, NULL, OPT_BURST_LIMIT},
268 {"out-of-band", no_argument, NULL, OPT_OUT_OF_BAND},
269 {"in-band", no_argument, NULL, OPT_IN_BAND},
270 {"netflow", required_argument, NULL, OPT_NETFLOW},
271 {"ports", required_argument, NULL, OPT_PORTS},
272 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
273 {"enable-dummy", no_argument, NULL, OPT_ENABLE_DUMMY},
274 {"verbose", optional_argument, NULL, 'v'},
275 {"help", no_argument, NULL, 'h'},
276 {"version", no_argument, NULL, 'V'},
279 LEAK_CHECKER_LONG_OPTIONS,
280 STREAM_SSL_LONG_OPTIONS,
281 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
284 char *short_options = long_options_to_short_options(long_options);
285 struct ofproto_controller controller_opts;
286 struct sset controllers;
290 /* Set defaults that we can figure out before parsing options. */
291 controller_opts.target = NULL;
292 controller_opts.max_backoff = 8;
293 controller_opts.probe_interval = 5;
294 controller_opts.band = OFPROTO_IN_BAND;
295 controller_opts.rate_limit = 0;
296 controller_opts.burst_limit = 0;
297 s->unixctl_path = NULL;
298 s->fail_mode = OFPROTO_FAIL_STANDALONE;
303 s->serial_desc = NULL;
305 sset_init(&controllers);
306 sset_init(&s->snoops);
308 sset_init(&s->netflow);
309 sset_init(&s->ports);
313 c = getopt_long(argc, argv, short_options, long_options, NULL);
319 case OPT_DATAPATH_ID:
320 if (!dpid_from_string(optarg, &s->datapath_id)) {
321 VLOG_FATAL("argument to --datapath-id must be exactly 16 hex "
322 "digits and may not be all-zero");
327 s->mfr_desc = optarg;
338 case OPT_SERIAL_DESC:
339 s->serial_desc = optarg;
347 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
348 s->fail_mode = OFPROTO_FAIL_STANDALONE;
349 } else if (!strcmp(optarg, "closed")
350 || !strcmp(optarg, "secure")) {
351 s->fail_mode = OFPROTO_FAIL_SECURE;
353 VLOG_FATAL("--fail argument must be \"standalone\" "
358 case OPT_INACTIVITY_PROBE:
359 controller_opts.probe_interval = atoi(optarg);
360 if (controller_opts.probe_interval < 5) {
361 VLOG_FATAL("--inactivity-probe argument must be at least 5");
366 if (!strcmp(optarg, "permanent")) {
367 s->max_idle = OFP_FLOW_PERMANENT;
369 s->max_idle = atoi(optarg);
370 if (s->max_idle < 1 || s->max_idle > 65535) {
371 VLOG_FATAL("--max-idle argument must be between 1 and "
372 "65535 or the word 'permanent'");
377 case OPT_MAX_BACKOFF:
378 controller_opts.max_backoff = atoi(optarg);
379 if (controller_opts.max_backoff < 1) {
380 VLOG_FATAL("--max-backoff argument must be at least 1");
381 } else if (controller_opts.max_backoff > 3600) {
382 controller_opts.max_backoff = 3600;
388 controller_opts.rate_limit = atoi(optarg);
389 if (controller_opts.rate_limit < 1) {
390 VLOG_FATAL("--rate-limit argument must be at least 1");
393 controller_opts.rate_limit = 1000;
397 case OPT_BURST_LIMIT:
398 controller_opts.burst_limit = atoi(optarg);
399 if (controller_opts.burst_limit < 1) {
400 VLOG_FATAL("--burst-limit argument must be at least 1");
404 case OPT_OUT_OF_BAND:
405 controller_opts.band = OFPROTO_OUT_OF_BAND;
409 controller_opts.band = OFPROTO_IN_BAND;
413 sset_add(&s->netflow, optarg);
417 sset_add(&controllers, optarg);
421 sset_add(&s->snoops, optarg);
425 parse_ports(optarg, &s->ports);
429 s->unixctl_path = optarg;
432 case OPT_ENABLE_DUMMY:
440 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
443 DAEMON_OPTION_HANDLERS
447 LEAK_CHECKER_OPTION_HANDLERS
449 STREAM_SSL_OPTION_HANDLERS
451 case OPT_BOOTSTRAP_CA_CERT:
452 stream_ssl_set_ca_cert_file(optarg, true);
467 VLOG_FATAL("need at least two non-option arguments; "
468 "use --help for usage");
472 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
473 VLOG_WARN("Rate limit set to unusually low value %d",
474 controller_opts.rate_limit);
478 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
480 /* Figure out controller names. */
481 s->run_forever = false;
482 if (sset_is_empty(&controllers)) {
483 sset_add_and_free(&controllers, xasprintf("punix:%s/%s.mgmt",
484 ovs_rundir(), s->dp_name));
486 for (i = 1; i < argc; i++) {
487 if (!strcmp(argv[i], "none")) {
488 s->run_forever = true;
490 sset_add(&controllers, argv[i]);
494 /* Set up controllers. */
495 s->n_controllers = sset_count(&controllers);
496 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
498 SSET_FOR_EACH (name, &controllers) {
499 s->controllers[i] = controller_opts;
500 s->controllers[i].target = xstrdup(name);
503 sset_destroy(&controllers);
509 printf("%s: an OpenFlow switch implementation.\n"
510 "usage: %s [OPTIONS] [TYPE@]DATAPATH CONTROLLER...\n"
511 "where DATAPATH is a local datapath (e.g. \"dp0\")\n"
512 "optionally with an explicit TYPE (default: \"system\").\n"
513 "Each CONTROLLER is an active OpenFlow connection method.\n",
514 program_name, program_name);
515 vconn_usage(true, true, true);
516 printf("\nOpenFlow options:\n"
517 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
518 " (ID must consist of 16 hex digits)\n"
519 " --mfr-desc=MFR Identify manufacturer as MFR\n"
520 " --hw-desc=HW Identify hardware as HW\n"
521 " --sw-desc=SW Identify software as SW\n"
522 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
523 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
524 "\nNetworking options:\n"
525 " --fail=open|closed when controller connection fails:\n"
526 " closed: drop all packets\n"
527 " open (default): act as learning switch\n"
528 " --inactivity-probe=SECS time between inactivity probes\n"
529 " --max-idle=SECS max idle for flows set up by switch\n"
530 " --max-backoff=SECS max time between controller connection\n"
531 " attempts (default: 8 seconds)\n"
532 " -l, --listen=METHOD allow management connections on METHOD\n"
533 " (a passive OpenFlow connection method)\n"
534 " --snoop=METHOD allow controller snooping on METHOD\n"
535 " (a passive OpenFlow connection method)\n"
536 " --out-of-band controller connection is out-of-band\n"
537 " --netflow=HOST:PORT configure NetFlow output target\n"
538 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
539 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
540 " --burst-limit=BURST limit on packet credit for idle time\n");
543 printf("\nOther options:\n"
544 " --unixctl=SOCKET override default control socket name\n"
545 " -h, --help display this help message\n"
546 " -V, --version display version information\n");
547 leak_checker_usage();