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"
50 VLOG_DEFINE_THIS_MODULE(openflowd);
52 /* Settings that may be configured by the user. */
54 const char *unixctl_path; /* File name for unixctl socket. */
56 /* Controller configuration. */
57 struct ofproto_controller *controllers;
59 enum ofproto_fail_mode fail_mode;
60 bool run_forever; /* Continue running even with no controller? */
63 uint64_t datapath_id; /* Datapath ID. */
64 char *dp_name; /* Name of local datapath. */
65 char *dp_type; /* Type 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. */
73 const char *dp_desc; /* Datapath description. */
75 /* Related vconns and network devices. */
76 struct svec snoops; /* Listen for controller snooping conns. */
78 /* Failure behavior. */
79 int max_idle; /* Idle time for flows in fail-open mode. */
82 struct svec netflow; /* NetFlow targets. */
85 static unixctl_cb_func ovs_openflowd_exit;
87 static void parse_options(int argc, char *argv[], struct ofsettings *);
88 static void usage(void) NO_RETURN;
91 main(int argc, char *argv[])
93 struct unixctl_server *unixctl;
94 struct ofproto *ofproto;
98 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);
106 die_if_already_running();
109 /* Start listening for ovs-appctl requests. */
110 error = unixctl_server_create(s.unixctl_path, &unixctl);
115 unixctl_command_register("exit", ovs_openflowd_exit, &exiting);
117 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
118 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
120 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
122 ovs_fatal(error, "could not create datapath");
125 /* Add ports to the datapath if requested by the user. */
130 SVEC_FOR_EACH (i, port, &s.ports) {
131 struct netdev *netdev;
133 error = netdev_open_default(port, &netdev);
135 ovs_fatal(error, "%s: failed to open network device", port);
138 error = dpif_port_add(dpif, netdev, NULL);
140 ovs_fatal(error, "failed to add %s as a port", port);
143 netdev_close(netdev);
147 /* Start OpenFlow processing. */
148 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
150 ovs_fatal(error, "could not initialize openflow switch");
153 ofproto_set_datapath_id(ofproto, s.datapath_id);
155 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
156 s.serial_desc, s.dp_desc);
157 error = ofproto_set_snoops(ofproto, &s.snoops);
160 "failed to configure controller snooping connections");
162 memset(&nf_options, 0, sizeof nf_options);
163 nf_options.collectors = s.netflow;
164 error = ofproto_set_netflow(ofproto, &nf_options);
166 ovs_fatal(error, "failed to configure NetFlow collectors");
168 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
169 ofproto_set_fail_mode(ofproto, s.fail_mode);
171 daemonize_complete();
174 while (!exiting && (s.run_forever || ofproto_is_alive(ofproto))) {
175 error = ofproto_run(ofproto);
177 ovs_fatal(error, "unrecoverable datapath error");
179 unixctl_server_run(unixctl);
183 ofproto_wait(ofproto);
184 unixctl_server_wait(unixctl);
188 poll_immediate_wake();
199 ovs_openflowd_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
202 bool *exiting = exiting_;
204 unixctl_command_reply(conn, 200, NULL);
207 /* User interface. */
210 parse_options(int argc, char *argv[], struct ofsettings *s)
213 OPT_DATAPATH_ID = UCHAR_MAX + 1,
221 OPT_INACTIVITY_PROBE,
227 OPT_BOOTSTRAP_CA_CERT,
235 LEAK_CHECKER_OPTION_ENUMS,
238 static struct option long_options[] = {
239 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
240 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
241 {"hw-desc", required_argument, 0, OPT_HW_DESC},
242 {"sw-desc", required_argument, 0, OPT_SW_DESC},
243 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
244 {"dp-desc", required_argument, 0, OPT_DP_DESC},
245 {"config", required_argument, 0, 'F'},
246 {"br-name", required_argument, 0, OPT_BR_NAME},
247 {"fail", required_argument, 0, OPT_FAIL_MODE},
248 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
249 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
250 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
251 {"listen", required_argument, 0, 'l'},
252 {"snoop", required_argument, 0, OPT_SNOOP},
253 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
254 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
255 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
256 {"in-band", no_argument, 0, OPT_IN_BAND},
257 {"netflow", required_argument, 0, OPT_NETFLOW},
258 {"ports", required_argument, 0, OPT_PORTS},
259 {"unixctl", required_argument, 0, OPT_UNIXCTL},
260 {"enable-dummy", no_argument, 0, OPT_ENABLE_DUMMY},
261 {"verbose", optional_argument, 0, 'v'},
262 {"help", no_argument, 0, 'h'},
263 {"version", no_argument, 0, 'V'},
266 LEAK_CHECKER_LONG_OPTIONS,
268 STREAM_SSL_LONG_OPTIONS
269 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
273 char *short_options = long_options_to_short_options(long_options);
274 struct ofproto_controller controller_opts;
275 struct svec controllers;
278 /* Set defaults that we can figure out before parsing options. */
279 controller_opts.target = NULL;
280 controller_opts.max_backoff = 8;
281 controller_opts.probe_interval = 5;
282 controller_opts.band = OFPROTO_IN_BAND;
283 controller_opts.rate_limit = 0;
284 controller_opts.burst_limit = 0;
285 s->unixctl_path = NULL;
286 s->fail_mode = OFPROTO_FAIL_STANDALONE;
291 s->serial_desc = NULL;
293 svec_init(&controllers);
294 svec_init(&s->snoops);
296 svec_init(&s->netflow);
297 svec_init(&s->ports);
301 c = getopt_long(argc, argv, short_options, long_options, NULL);
307 case OPT_DATAPATH_ID:
308 if (!dpid_from_string(optarg, &s->datapath_id)) {
309 ovs_fatal(0, "argument to --datapath-id must be "
310 "exactly 16 hex digits and may not be all-zero");
315 s->mfr_desc = optarg;
326 case OPT_SERIAL_DESC:
327 s->serial_desc = optarg;
335 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
336 s->fail_mode = OFPROTO_FAIL_STANDALONE;
337 } else if (!strcmp(optarg, "closed")
338 || !strcmp(optarg, "secure")) {
339 s->fail_mode = OFPROTO_FAIL_SECURE;
341 ovs_fatal(0, "--fail argument must be \"standalone\" "
346 case OPT_INACTIVITY_PROBE:
347 controller_opts.probe_interval = atoi(optarg);
348 if (controller_opts.probe_interval < 5) {
349 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
354 if (!strcmp(optarg, "permanent")) {
355 s->max_idle = OFP_FLOW_PERMANENT;
357 s->max_idle = atoi(optarg);
358 if (s->max_idle < 1 || s->max_idle > 65535) {
359 ovs_fatal(0, "--max-idle argument must be between 1 and "
360 "65535 or the word 'permanent'");
365 case OPT_MAX_BACKOFF:
366 controller_opts.max_backoff = atoi(optarg);
367 if (controller_opts.max_backoff < 1) {
368 ovs_fatal(0, "--max-backoff argument must be at least 1");
369 } else if (controller_opts.max_backoff > 3600) {
370 controller_opts.max_backoff = 3600;
376 controller_opts.rate_limit = atoi(optarg);
377 if (controller_opts.rate_limit < 1) {
378 ovs_fatal(0, "--rate-limit argument must be at least 1");
381 controller_opts.rate_limit = 1000;
385 case OPT_BURST_LIMIT:
386 controller_opts.burst_limit = atoi(optarg);
387 if (controller_opts.burst_limit < 1) {
388 ovs_fatal(0, "--burst-limit argument must be at least 1");
392 case OPT_OUT_OF_BAND:
393 controller_opts.band = OFPROTO_OUT_OF_BAND;
397 controller_opts.band = OFPROTO_IN_BAND;
401 svec_add(&s->netflow, optarg);
405 svec_add(&controllers, optarg);
409 svec_add(&s->snoops, optarg);
413 svec_split(&s->ports, optarg, ",");
417 s->unixctl_path = optarg;
420 case OPT_ENABLE_DUMMY:
428 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
431 DAEMON_OPTION_HANDLERS
435 LEAK_CHECKER_OPTION_HANDLERS
438 STREAM_SSL_OPTION_HANDLERS
440 case OPT_BOOTSTRAP_CA_CERT:
441 stream_ssl_set_ca_cert_file(optarg, true);
457 ovs_fatal(0, "need at least two non-option arguments; "
458 "use --help for usage");
462 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
463 VLOG_WARN("Rate limit set to unusually low value %d",
464 controller_opts.rate_limit);
468 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
470 /* Figure out controller names. */
471 s->run_forever = false;
472 if (!controllers.n) {
473 svec_add_nocopy(&controllers, xasprintf("punix:%s/%s.mgmt",
474 ovs_rundir(), s->dp_name));
476 for (i = 1; i < argc; i++) {
477 if (!strcmp(argv[i], "none")) {
478 s->run_forever = true;
480 svec_add(&controllers, argv[i]);
484 /* Set up controllers. */
485 s->n_controllers = controllers.n;
486 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
487 for (i = 0; i < s->n_controllers; i++) {
488 s->controllers[i] = controller_opts;
489 s->controllers[i].target = controllers.names[i];
496 printf("%s: an OpenFlow switch implementation.\n"
497 "usage: %s [OPTIONS] [TYPE@]DATAPATH CONTROLLER...\n"
498 "where DATAPATH is a local datapath (e.g. \"dp0\")\n"
499 "optionally with an explicit TYPE (default: \"system\").\n"
500 "Each CONTROLLER is an active OpenFlow connection method.\n",
501 program_name, program_name);
502 vconn_usage(true, true, true);
503 printf("\nOpenFlow options:\n"
504 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
505 " (ID must consist of 16 hex digits)\n"
506 " --mfr-desc=MFR Identify manufacturer as MFR\n"
507 " --hw-desc=HW Identify hardware as HW\n"
508 " --sw-desc=SW Identify software as SW\n"
509 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
510 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
511 "\nNetworking options:\n"
512 " --fail=open|closed when controller connection fails:\n"
513 " closed: drop all packets\n"
514 " open (default): act as learning switch\n"
515 " --inactivity-probe=SECS time between inactivity probes\n"
516 " --max-idle=SECS max idle for flows set up by switch\n"
517 " --max-backoff=SECS max time between controller connection\n"
518 " attempts (default: 8 seconds)\n"
519 " -l, --listen=METHOD allow management connections on METHOD\n"
520 " (a passive OpenFlow connection method)\n"
521 " --snoop=METHOD allow controller snooping on METHOD\n"
522 " (a passive OpenFlow connection method)\n"
523 " --out-of-band controller connection is out-of-band\n"
524 " --netflow=HOST:PORT configure NetFlow output target\n"
525 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
526 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
527 " --burst-limit=BURST limit on packet credit for idle time\n");
530 printf("\nOther options:\n"
531 " --unixctl=SOCKET override default control socket name\n"
532 " -h, --help display this help message\n"
533 " -V, --version display version information\n");
534 leak_checker_usage();