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 snoops; /* Listen for controller snooping conns. */
74 /* Failure behavior. */
75 int max_idle; /* Idle time for flows in fail-open mode. */
78 struct svec netflow; /* NetFlow targets. */
81 static void parse_options(int argc, char *argv[], struct ofsettings *);
82 static void usage(void) NO_RETURN;
85 main(int argc, char *argv[])
87 struct unixctl_server *unixctl;
88 struct ofproto *ofproto;
92 struct netflow_options nf_options;
94 proctitle_init(argc, argv);
95 set_program_name(argv[0]);
96 parse_options(argc, argv, &s);
97 signal(SIGPIPE, SIG_IGN);
99 die_if_already_running();
102 /* Start listening for ovs-appctl requests. */
103 error = unixctl_server_create(NULL, &unixctl);
108 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
109 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
111 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
113 ovs_fatal(error, "could not create datapath");
116 /* Add ports to the datapath if requested by the user. */
121 SVEC_FOR_EACH (i, port, &s.ports) {
122 error = dpif_port_add(dpif, port, 0, NULL);
124 ovs_fatal(error, "failed to add %s as a port", port);
129 /* Start OpenFlow processing. */
130 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
132 ovs_fatal(error, "could not initialize openflow switch");
135 ofproto_set_datapath_id(ofproto, s.datapath_id);
137 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
138 s.serial_desc, s.dp_desc);
139 error = ofproto_set_snoops(ofproto, &s.snoops);
142 "failed to configure controller snooping connections");
144 memset(&nf_options, 0, sizeof nf_options);
145 nf_options.collectors = s.netflow;
146 error = ofproto_set_netflow(ofproto, &nf_options);
148 ovs_fatal(error, "failed to configure NetFlow collectors");
150 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
151 ofproto_set_fail_mode(ofproto, s.fail_mode);
153 daemonize_complete();
155 while (ofproto_is_alive(ofproto)) {
156 error = ofproto_run(ofproto);
158 ovs_fatal(error, "unrecoverable datapath error");
160 unixctl_server_run(unixctl);
164 ofproto_wait(ofproto);
165 unixctl_server_wait(unixctl);
176 /* User interface. */
179 parse_options(int argc, char *argv[], struct ofsettings *s)
182 OPT_DATAPATH_ID = UCHAR_MAX + 1,
192 OPT_INACTIVITY_PROBE,
198 OPT_BOOTSTRAP_CA_CERT,
204 LEAK_CHECKER_OPTION_ENUMS
206 static struct option long_options[] = {
207 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
208 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
209 {"hw-desc", required_argument, 0, OPT_HW_DESC},
210 {"sw-desc", required_argument, 0, OPT_SW_DESC},
211 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
212 {"dp-desc", required_argument, 0, OPT_DP_DESC},
213 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
214 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
215 {"config", required_argument, 0, 'F'},
216 {"br-name", required_argument, 0, OPT_BR_NAME},
217 {"fail", required_argument, 0, OPT_FAIL_MODE},
218 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
219 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
220 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
221 {"listen", required_argument, 0, 'l'},
222 {"snoop", required_argument, 0, OPT_SNOOP},
223 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
224 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
225 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
226 {"in-band", no_argument, 0, OPT_IN_BAND},
227 {"netflow", required_argument, 0, OPT_NETFLOW},
228 {"ports", required_argument, 0, OPT_PORTS},
229 {"verbose", optional_argument, 0, 'v'},
230 {"help", no_argument, 0, 'h'},
231 {"version", no_argument, 0, 'V'},
234 LEAK_CHECKER_LONG_OPTIONS,
236 STREAM_SSL_LONG_OPTIONS
237 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
241 char *short_options = long_options_to_short_options(long_options);
242 struct ofproto_controller controller_opts;
243 struct svec controllers;
246 /* Set defaults that we can figure out before parsing options. */
247 controller_opts.target = NULL;
248 controller_opts.max_backoff = 8;
249 controller_opts.probe_interval = 5;
250 controller_opts.band = OFPROTO_IN_BAND;
251 controller_opts.accept_re = NULL;
252 controller_opts.update_resolv_conf = true;
253 controller_opts.rate_limit = 0;
254 controller_opts.burst_limit = 0;
255 s->fail_mode = OFPROTO_FAIL_STANDALONE;
260 s->serial_desc = NULL;
262 svec_init(&controllers);
263 svec_init(&s->snoops);
265 svec_init(&s->netflow);
266 svec_init(&s->ports);
270 c = getopt_long(argc, argv, short_options, long_options, NULL);
276 case OPT_DATAPATH_ID:
277 if (!dpid_from_string(optarg, &s->datapath_id)) {
278 ovs_fatal(0, "argument to --datapath-id must be "
279 "exactly 16 hex digits and may not be all-zero");
284 s->mfr_desc = optarg;
295 case OPT_SERIAL_DESC:
296 s->serial_desc = optarg;
303 case OPT_ACCEPT_VCONN:
304 controller_opts.accept_re = optarg;
307 case OPT_NO_RESOLV_CONF:
308 controller_opts.update_resolv_conf = false;
312 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
313 s->fail_mode = OFPROTO_FAIL_STANDALONE;
314 } else if (!strcmp(optarg, "closed")
315 || !strcmp(optarg, "secure")) {
316 s->fail_mode = OFPROTO_FAIL_SECURE;
318 ovs_fatal(0, "--fail argument must be \"standalone\" "
323 case OPT_INACTIVITY_PROBE:
324 controller_opts.probe_interval = atoi(optarg);
325 if (controller_opts.probe_interval < 5) {
326 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
331 if (!strcmp(optarg, "permanent")) {
332 s->max_idle = OFP_FLOW_PERMANENT;
334 s->max_idle = atoi(optarg);
335 if (s->max_idle < 1 || s->max_idle > 65535) {
336 ovs_fatal(0, "--max-idle argument must be between 1 and "
337 "65535 or the word 'permanent'");
342 case OPT_MAX_BACKOFF:
343 controller_opts.max_backoff = atoi(optarg);
344 if (controller_opts.max_backoff < 1) {
345 ovs_fatal(0, "--max-backoff argument must be at least 1");
346 } else if (controller_opts.max_backoff > 3600) {
347 controller_opts.max_backoff = 3600;
353 controller_opts.rate_limit = atoi(optarg);
354 if (controller_opts.rate_limit < 1) {
355 ovs_fatal(0, "--rate-limit argument must be at least 1");
358 controller_opts.rate_limit = 1000;
362 case OPT_BURST_LIMIT:
363 controller_opts.burst_limit = atoi(optarg);
364 if (controller_opts.burst_limit < 1) {
365 ovs_fatal(0, "--burst-limit argument must be at least 1");
369 case OPT_OUT_OF_BAND:
370 controller_opts.band = OFPROTO_OUT_OF_BAND;
374 controller_opts.band = OFPROTO_IN_BAND;
378 svec_add(&s->netflow, optarg);
382 svec_add(&controllers, optarg);
386 svec_add(&s->snoops, optarg);
390 svec_split(&s->ports, optarg, ",");
397 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
400 DAEMON_OPTION_HANDLERS
404 LEAK_CHECKER_OPTION_HANDLERS
407 STREAM_SSL_OPTION_HANDLERS
409 case OPT_BOOTSTRAP_CA_CERT:
410 stream_ssl_set_ca_cert_file(optarg, true);
426 ovs_fatal(0, "need at least one non-option arguments; "
427 "use --help for usage");
430 /* Set accept_controller_regex. */
431 if (!controller_opts.accept_re) {
432 controller_opts.accept_re
433 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
437 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
438 VLOG_WARN("Rate limit set to unusually low value %d",
439 controller_opts.rate_limit);
443 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
445 /* Figure out controller names. */
446 if (!controllers.n) {
447 svec_add_nocopy(&controllers, xasprintf("punix:%s/%s.mgmt",
448 ovs_rundir(), s->dp_name));
450 for (i = 1; i < argc; i++) {
451 svec_add(&controllers, argv[i]);
454 svec_add(&controllers, "discover");
457 /* Set up controllers. */
458 s->n_controllers = controllers.n;
459 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
460 for (i = 0; i < s->n_controllers; i++) {
461 s->controllers[i] = controller_opts;
462 s->controllers[i].target = controllers.names[i];
466 if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
467 for (i = 0; i < s->n_controllers; i++) {
468 if (!strcmp(s->controllers[i].target, "discover")) {
469 ovs_fatal(0, "Cannot perform discovery with out-of-band "
479 printf("%s: an OpenFlow switch implementation.\n"
480 "usage: %s [OPTIONS] DATAPATH [CONTROLLER...]\n"
481 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
482 "Each CONTROLLER is an active OpenFlow connection method. If\n"
483 "none is given, ovs-openflowd performs controller discovery.\n",
484 program_name, program_name);
485 vconn_usage(true, true, true);
486 printf("\nOpenFlow options:\n"
487 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
488 " (ID must consist of 16 hex digits)\n"
489 " --mfr-desc=MFR Identify manufacturer as MFR\n"
490 " --hw-desc=HW Identify hardware as HW\n"
491 " --sw-desc=SW Identify software as SW\n"
492 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
493 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
494 "\nController discovery options:\n"
495 " --accept-vconn=REGEX accept matching discovered controllers\n"
496 " --no-resolv-conf do not update /etc/resolv.conf\n"
497 "\nNetworking options:\n"
498 " --fail=open|closed when controller connection fails:\n"
499 " closed: drop all packets\n"
500 " open (default): act as learning switch\n"
501 " --inactivity-probe=SECS time between inactivity probes\n"
502 " --max-idle=SECS max idle for flows set up by switch\n"
503 " --max-backoff=SECS max time between controller connection\n"
504 " attempts (default: 8 seconds)\n"
505 " -l, --listen=METHOD allow management connections on METHOD\n"
506 " (a passive OpenFlow connection method)\n"
507 " --snoop=METHOD allow controller snooping on METHOD\n"
508 " (a passive OpenFlow connection method)\n"
509 " --out-of-band controller connection is out-of-band\n"
510 " --netflow=HOST:PORT configure NetFlow output target\n"
511 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
512 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
513 " --burst-limit=BURST limit on packet credit for idle time\n");
516 printf("\nOther options:\n"
517 " -h, --help display this help message\n"
518 " -V, --version display version information\n");
519 leak_checker_usage();