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 const char *unixctl_path; /* File name for unixctl socket. */
55 /* Controller configuration. */
56 struct ofproto_controller *controllers;
58 enum ofproto_fail_mode fail_mode;
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 svec 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 svec snoops; /* Listen for controller snooping conns. */
76 /* Failure behavior. */
77 int max_idle; /* Idle time for flows in fail-open mode. */
80 struct svec netflow; /* NetFlow targets. */
83 static void parse_options(int argc, char *argv[], struct ofsettings *);
84 static void usage(void) NO_RETURN;
87 main(int argc, char *argv[])
89 struct unixctl_server *unixctl;
90 struct ofproto *ofproto;
94 struct netflow_options nf_options;
96 proctitle_init(argc, argv);
97 set_program_name(argv[0]);
98 parse_options(argc, argv, &s);
99 signal(SIGPIPE, SIG_IGN);
101 die_if_already_running();
104 /* Start listening for ovs-appctl requests. */
105 error = unixctl_server_create(s.unixctl_path, &unixctl);
110 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
111 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
113 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
115 ovs_fatal(error, "could not create datapath");
118 /* Add ports to the datapath if requested by the user. */
123 SVEC_FOR_EACH (i, port, &s.ports) {
124 error = dpif_port_add(dpif, port, 0, NULL);
126 ovs_fatal(error, "failed to add %s as a port", port);
131 /* Start OpenFlow processing. */
132 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
134 ovs_fatal(error, "could not initialize openflow switch");
137 ofproto_set_datapath_id(ofproto, s.datapath_id);
139 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
140 s.serial_desc, s.dp_desc);
141 error = ofproto_set_snoops(ofproto, &s.snoops);
144 "failed to configure controller snooping connections");
146 memset(&nf_options, 0, sizeof nf_options);
147 nf_options.collectors = s.netflow;
148 error = ofproto_set_netflow(ofproto, &nf_options);
150 ovs_fatal(error, "failed to configure NetFlow collectors");
152 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
153 ofproto_set_fail_mode(ofproto, s.fail_mode);
155 daemonize_complete();
157 while (ofproto_is_alive(ofproto)) {
158 error = ofproto_run(ofproto);
160 ovs_fatal(error, "unrecoverable datapath error");
162 unixctl_server_run(unixctl);
166 ofproto_wait(ofproto);
167 unixctl_server_wait(unixctl);
178 /* User interface. */
181 parse_options(int argc, char *argv[], struct ofsettings *s)
184 OPT_DATAPATH_ID = UCHAR_MAX + 1,
194 OPT_INACTIVITY_PROBE,
200 OPT_BOOTSTRAP_CA_CERT,
207 LEAK_CHECKER_OPTION_ENUMS
209 static struct option long_options[] = {
210 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
211 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
212 {"hw-desc", required_argument, 0, OPT_HW_DESC},
213 {"sw-desc", required_argument, 0, OPT_SW_DESC},
214 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
215 {"dp-desc", required_argument, 0, OPT_DP_DESC},
216 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
217 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
218 {"config", required_argument, 0, 'F'},
219 {"br-name", required_argument, 0, OPT_BR_NAME},
220 {"fail", required_argument, 0, OPT_FAIL_MODE},
221 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
222 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
223 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
224 {"listen", required_argument, 0, 'l'},
225 {"snoop", required_argument, 0, OPT_SNOOP},
226 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
227 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
228 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
229 {"in-band", no_argument, 0, OPT_IN_BAND},
230 {"netflow", required_argument, 0, OPT_NETFLOW},
231 {"ports", required_argument, 0, OPT_PORTS},
232 {"unixctl", required_argument, 0, OPT_UNIXCTL},
233 {"verbose", optional_argument, 0, 'v'},
234 {"help", no_argument, 0, 'h'},
235 {"version", no_argument, 0, 'V'},
238 LEAK_CHECKER_LONG_OPTIONS,
240 STREAM_SSL_LONG_OPTIONS
241 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
245 char *short_options = long_options_to_short_options(long_options);
246 struct ofproto_controller controller_opts;
247 struct svec controllers;
250 /* Set defaults that we can figure out before parsing options. */
251 controller_opts.target = NULL;
252 controller_opts.max_backoff = 8;
253 controller_opts.probe_interval = 5;
254 controller_opts.band = OFPROTO_IN_BAND;
255 controller_opts.accept_re = NULL;
256 controller_opts.update_resolv_conf = true;
257 controller_opts.rate_limit = 0;
258 controller_opts.burst_limit = 0;
259 s->unixctl_path = NULL;
260 s->fail_mode = OFPROTO_FAIL_STANDALONE;
265 s->serial_desc = NULL;
267 svec_init(&controllers);
268 svec_init(&s->snoops);
270 svec_init(&s->netflow);
271 svec_init(&s->ports);
275 c = getopt_long(argc, argv, short_options, long_options, NULL);
281 case OPT_DATAPATH_ID:
282 if (!dpid_from_string(optarg, &s->datapath_id)) {
283 ovs_fatal(0, "argument to --datapath-id must be "
284 "exactly 16 hex digits and may not be all-zero");
289 s->mfr_desc = optarg;
300 case OPT_SERIAL_DESC:
301 s->serial_desc = optarg;
308 case OPT_ACCEPT_VCONN:
309 controller_opts.accept_re = optarg;
312 case OPT_NO_RESOLV_CONF:
313 controller_opts.update_resolv_conf = false;
317 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
318 s->fail_mode = OFPROTO_FAIL_STANDALONE;
319 } else if (!strcmp(optarg, "closed")
320 || !strcmp(optarg, "secure")) {
321 s->fail_mode = OFPROTO_FAIL_SECURE;
323 ovs_fatal(0, "--fail argument must be \"standalone\" "
328 case OPT_INACTIVITY_PROBE:
329 controller_opts.probe_interval = atoi(optarg);
330 if (controller_opts.probe_interval < 5) {
331 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
336 if (!strcmp(optarg, "permanent")) {
337 s->max_idle = OFP_FLOW_PERMANENT;
339 s->max_idle = atoi(optarg);
340 if (s->max_idle < 1 || s->max_idle > 65535) {
341 ovs_fatal(0, "--max-idle argument must be between 1 and "
342 "65535 or the word 'permanent'");
347 case OPT_MAX_BACKOFF:
348 controller_opts.max_backoff = atoi(optarg);
349 if (controller_opts.max_backoff < 1) {
350 ovs_fatal(0, "--max-backoff argument must be at least 1");
351 } else if (controller_opts.max_backoff > 3600) {
352 controller_opts.max_backoff = 3600;
358 controller_opts.rate_limit = atoi(optarg);
359 if (controller_opts.rate_limit < 1) {
360 ovs_fatal(0, "--rate-limit argument must be at least 1");
363 controller_opts.rate_limit = 1000;
367 case OPT_BURST_LIMIT:
368 controller_opts.burst_limit = atoi(optarg);
369 if (controller_opts.burst_limit < 1) {
370 ovs_fatal(0, "--burst-limit argument must be at least 1");
374 case OPT_OUT_OF_BAND:
375 controller_opts.band = OFPROTO_OUT_OF_BAND;
379 controller_opts.band = OFPROTO_IN_BAND;
383 svec_add(&s->netflow, optarg);
387 svec_add(&controllers, optarg);
391 svec_add(&s->snoops, optarg);
395 svec_split(&s->ports, optarg, ",");
399 s->unixctl_path = optarg;
406 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
409 DAEMON_OPTION_HANDLERS
413 LEAK_CHECKER_OPTION_HANDLERS
416 STREAM_SSL_OPTION_HANDLERS
418 case OPT_BOOTSTRAP_CA_CERT:
419 stream_ssl_set_ca_cert_file(optarg, true);
435 ovs_fatal(0, "need at least one non-option arguments; "
436 "use --help for usage");
439 /* Set accept_controller_regex. */
440 if (!controller_opts.accept_re) {
441 controller_opts.accept_re
442 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
446 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
447 VLOG_WARN("Rate limit set to unusually low value %d",
448 controller_opts.rate_limit);
452 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
454 /* Figure out controller names. */
455 if (!controllers.n) {
456 svec_add_nocopy(&controllers, xasprintf("punix:%s/%s.mgmt",
457 ovs_rundir(), s->dp_name));
459 for (i = 1; i < argc; i++) {
460 svec_add(&controllers, argv[i]);
463 svec_add(&controllers, "discover");
466 /* Set up controllers. */
467 s->n_controllers = controllers.n;
468 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
469 for (i = 0; i < s->n_controllers; i++) {
470 s->controllers[i] = controller_opts;
471 s->controllers[i].target = controllers.names[i];
475 if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
476 for (i = 0; i < s->n_controllers; i++) {
477 if (!strcmp(s->controllers[i].target, "discover")) {
478 ovs_fatal(0, "Cannot perform discovery with out-of-band "
488 printf("%s: an OpenFlow switch implementation.\n"
489 "usage: %s [OPTIONS] DATAPATH [CONTROLLER...]\n"
490 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
491 "Each CONTROLLER is an active OpenFlow connection method. If\n"
492 "none is given, ovs-openflowd performs controller discovery.\n",
493 program_name, program_name);
494 vconn_usage(true, true, true);
495 printf("\nOpenFlow options:\n"
496 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
497 " (ID must consist of 16 hex digits)\n"
498 " --mfr-desc=MFR Identify manufacturer as MFR\n"
499 " --hw-desc=HW Identify hardware as HW\n"
500 " --sw-desc=SW Identify software as SW\n"
501 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
502 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
503 "\nController discovery options:\n"
504 " --accept-vconn=REGEX accept matching discovered controllers\n"
505 " --no-resolv-conf do not update /etc/resolv.conf\n"
506 "\nNetworking options:\n"
507 " --fail=open|closed when controller connection fails:\n"
508 " closed: drop all packets\n"
509 " open (default): act as learning switch\n"
510 " --inactivity-probe=SECS time between inactivity probes\n"
511 " --max-idle=SECS max idle for flows set up by switch\n"
512 " --max-backoff=SECS max time between controller connection\n"
513 " attempts (default: 8 seconds)\n"
514 " -l, --listen=METHOD allow management connections on METHOD\n"
515 " (a passive OpenFlow connection method)\n"
516 " --snoop=METHOD allow controller snooping on METHOD\n"
517 " (a passive OpenFlow connection method)\n"
518 " --out-of-band controller connection is out-of-band\n"
519 " --netflow=HOST:PORT configure NetFlow output target\n"
520 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
521 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
522 " --burst-limit=BURST limit on packet credit for idle time\n");
525 printf("\nOther options:\n"
526 " --unixctl=SOCKET override default control socket name\n"
527 " -h, --help display this help message\n"
528 " -V, --version display version information\n");
529 leak_checker_usage();