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"
31 #include "leak-checker.h"
35 #include "ofproto/ofproto.h"
36 #include "ofproto/wdp.h"
37 #include "openflow/openflow.h"
39 #include "poll-loop.h"
41 #include "stream-ssl.h"
50 VLOG_DEFINE_THIS_MODULE(openflowd)
52 /* Settings that may be configured by the user. */
54 /* Controller configuration. */
55 struct ofproto_controller *controllers;
57 enum ofproto_fail_mode fail_mode;
60 uint64_t datapath_id; /* Datapath ID. */
61 char *dp_name; /* Name of local datapath. */
62 char *dp_type; /* Type of local datapath. */
63 struct svec ports; /* Set of ports to add to datapath (if any). */
65 /* Description strings. */
66 const char *mfr_desc; /* Manufacturer. */
67 const char *hw_desc; /* Hardware. */
68 const char *sw_desc; /* Software version. */
69 const char *serial_desc; /* Serial number. */
70 const char *dp_desc; /* Datapath description. */
72 /* Related vconns and network devices. */
73 struct svec snoops; /* Listen for controller snooping conns. */
75 /* Failure behavior. */
76 int max_idle; /* Idle time for flows in fail-open mode. */
79 struct svec netflow; /* NetFlow targets. */
82 static void parse_options(int argc, char *argv[], struct ofsettings *);
83 static void usage(void) NO_RETURN;
86 main(int argc, char *argv[])
88 struct unixctl_server *unixctl;
89 struct ofproto *ofproto;
93 struct netflow_options nf_options;
95 proctitle_init(argc, argv);
96 set_program_name(argv[0]);
97 parse_options(argc, argv, &s);
98 signal(SIGPIPE, SIG_IGN);
100 die_if_already_running();
103 /* Start listening for ovs-appctl requests. */
104 error = unixctl_server_create(NULL, &unixctl);
109 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
110 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
112 error = xfif_create_and_open(s.dp_name, s.dp_type, &xfif);
114 ovs_fatal(error, "could not create datapath");
117 /* Add ports to the datapath if requested by the user. */
122 SVEC_FOR_EACH (i, port, &s.ports) {
123 error = xfif_port_add(xfif, port, 0, NULL);
125 ovs_fatal(error, "failed to add %s as a port", port);
130 /* Start OpenFlow processing. */
131 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
133 ovs_fatal(error, "could not initialize openflow switch");
136 ofproto_set_datapath_id(ofproto, s.datapath_id);
138 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
139 s.serial_desc, s.dp_desc);
140 error = ofproto_set_snoops(ofproto, &s.snoops);
143 "failed to configure controller snooping connections");
145 memset(&nf_options, 0, sizeof nf_options);
146 nf_options.collectors = s.netflow;
147 error = ofproto_set_netflow(ofproto, &nf_options);
149 ovs_fatal(error, "failed to configure NetFlow collectors");
151 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
152 ofproto_set_fail_mode(ofproto, s.fail_mode);
154 daemonize_complete();
156 while (ofproto_is_alive(ofproto)) {
157 error = ofproto_run(ofproto);
159 ovs_fatal(error, "unrecoverable datapath error");
161 unixctl_server_run(unixctl);
165 ofproto_wait(ofproto);
166 unixctl_server_wait(unixctl);
177 /* User interface. */
180 parse_options(int argc, char *argv[], struct ofsettings *s)
183 OPT_DATAPATH_ID = UCHAR_MAX + 1,
193 OPT_INACTIVITY_PROBE,
199 OPT_BOOTSTRAP_CA_CERT,
205 LEAK_CHECKER_OPTION_ENUMS
207 static struct option long_options[] = {
208 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
209 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
210 {"hw-desc", required_argument, 0, OPT_HW_DESC},
211 {"sw-desc", required_argument, 0, OPT_SW_DESC},
212 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
213 {"dp-desc", required_argument, 0, OPT_DP_DESC},
214 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
215 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
216 {"config", required_argument, 0, 'F'},
217 {"br-name", required_argument, 0, OPT_BR_NAME},
218 {"fail", required_argument, 0, OPT_FAIL_MODE},
219 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
220 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
221 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
222 {"listen", required_argument, 0, 'l'},
223 {"snoop", required_argument, 0, OPT_SNOOP},
224 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
225 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
226 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
227 {"in-band", no_argument, 0, OPT_IN_BAND},
228 {"netflow", required_argument, 0, OPT_NETFLOW},
229 {"ports", required_argument, 0, OPT_PORTS},
230 {"verbose", optional_argument, 0, 'v'},
231 {"help", no_argument, 0, 'h'},
232 {"version", no_argument, 0, 'V'},
235 LEAK_CHECKER_LONG_OPTIONS,
237 STREAM_SSL_LONG_OPTIONS
238 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
242 char *short_options = long_options_to_short_options(long_options);
243 struct ofproto_controller controller_opts;
244 struct svec controllers;
247 /* Set defaults that we can figure out before parsing options. */
248 controller_opts.target = NULL;
249 controller_opts.max_backoff = 8;
250 controller_opts.probe_interval = 5;
251 controller_opts.band = OFPROTO_IN_BAND;
252 controller_opts.accept_re = NULL;
253 controller_opts.update_resolv_conf = true;
254 controller_opts.rate_limit = 0;
255 controller_opts.burst_limit = 0;
256 s->fail_mode = OFPROTO_FAIL_STANDALONE;
261 s->serial_desc = NULL;
263 svec_init(&controllers);
264 svec_init(&s->snoops);
266 svec_init(&s->netflow);
267 svec_init(&s->ports);
271 c = getopt_long(argc, argv, short_options, long_options, NULL);
277 case OPT_DATAPATH_ID:
278 if (!dpid_from_string(optarg, &s->datapath_id)) {
279 ovs_fatal(0, "argument to --datapath-id must be "
280 "exactly 16 hex digits and may not be all-zero");
285 s->mfr_desc = optarg;
296 case OPT_SERIAL_DESC:
297 s->serial_desc = optarg;
304 case OPT_ACCEPT_VCONN:
305 controller_opts.accept_re = optarg;
308 case OPT_NO_RESOLV_CONF:
309 controller_opts.update_resolv_conf = false;
313 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
314 s->fail_mode = OFPROTO_FAIL_STANDALONE;
315 } else if (!strcmp(optarg, "closed")
316 || !strcmp(optarg, "secure")) {
317 s->fail_mode = OFPROTO_FAIL_SECURE;
319 ovs_fatal(0, "--fail argument must be \"standalone\" "
324 case OPT_INACTIVITY_PROBE:
325 controller_opts.probe_interval = atoi(optarg);
326 if (controller_opts.probe_interval < 5) {
327 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
332 if (!strcmp(optarg, "permanent")) {
333 s->max_idle = OFP_FLOW_PERMANENT;
335 s->max_idle = atoi(optarg);
336 if (s->max_idle < 1 || s->max_idle > 65535) {
337 ovs_fatal(0, "--max-idle argument must be between 1 and "
338 "65535 or the word 'permanent'");
343 case OPT_MAX_BACKOFF:
344 controller_opts.max_backoff = atoi(optarg);
345 if (controller_opts.max_backoff < 1) {
346 ovs_fatal(0, "--max-backoff argument must be at least 1");
347 } else if (controller_opts.max_backoff > 3600) {
348 controller_opts.max_backoff = 3600;
354 controller_opts.rate_limit = atoi(optarg);
355 if (controller_opts.rate_limit < 1) {
356 ovs_fatal(0, "--rate-limit argument must be at least 1");
359 controller_opts.rate_limit = 1000;
363 case OPT_BURST_LIMIT:
364 controller_opts.burst_limit = atoi(optarg);
365 if (controller_opts.burst_limit < 1) {
366 ovs_fatal(0, "--burst-limit argument must be at least 1");
370 case OPT_OUT_OF_BAND:
371 controller_opts.band = OFPROTO_OUT_OF_BAND;
375 controller_opts.band = OFPROTO_IN_BAND;
379 svec_add(&s->netflow, optarg);
383 svec_add(&controllers, optarg);
387 svec_add(&s->snoops, optarg);
391 svec_split(&s->ports, optarg, ",");
398 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
401 DAEMON_OPTION_HANDLERS
405 LEAK_CHECKER_OPTION_HANDLERS
408 STREAM_SSL_OPTION_HANDLERS
410 case OPT_BOOTSTRAP_CA_CERT:
411 stream_ssl_set_ca_cert_file(optarg, true);
427 ovs_fatal(0, "need at least one non-option arguments; "
428 "use --help for usage");
431 /* Set accept_controller_regex. */
432 if (!controller_opts.accept_re) {
433 controller_opts.accept_re
434 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
438 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
439 VLOG_WARN("Rate limit set to unusually low value %d",
440 controller_opts.rate_limit);
444 xf_parse_name(argv[0], &s->dp_name, &s->dp_type);
446 /* Figure out controller names. */
447 if (!controllers.n) {
448 svec_add_nocopy(&controllers,
449 xasprintf("punix:%s/%s.mgmt", ovs_rundir, s->dp_name));
451 for (i = 1; i < argc; i++) {
452 svec_add(&controllers, argv[i]);
455 svec_add(&controllers, "discover");
458 /* Set up controllers. */
459 s->n_controllers = controllers.n;
460 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
462 for (i = 0; i < s->n_controllers; i++) {
463 s->controllers[i] = controller_opts;
464 s->controllers[i].target = controllers.names[i];
469 if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
470 for (i = 0; i < s->n_controllers; i++) {
471 if (!strcmp(s->controllers[i].target, "discover")) {
472 ovs_fatal(0, "Cannot perform discovery with out-of-band "
482 printf("%s: an OpenFlow switch implementation.\n"
483 "usage: %s [OPTIONS] DATAPATH [CONTROLLER...]\n"
484 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
485 "Each CONTROLLER is an active OpenFlow connection method. If\n"
486 "none is given, ovs-openflowd performs controller discovery.\n",
487 program_name, program_name);
488 vconn_usage(true, true, true);
489 printf("\nOpenFlow options:\n"
490 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
491 " (ID must consist of 16 hex digits)\n"
492 " --mfr-desc=MFR Identify manufacturer as MFR\n"
493 " --hw-desc=HW Identify hardware as HW\n"
494 " --sw-desc=SW Identify software as SW\n"
495 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
496 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
497 "\nController discovery options:\n"
498 " --accept-vconn=REGEX accept matching discovered controllers\n"
499 " --no-resolv-conf do not update /etc/resolv.conf\n"
500 "\nNetworking options:\n"
501 " --fail=open|closed when controller connection fails:\n"
502 " closed: drop all packets\n"
503 " open (default): act as learning switch\n"
504 " --inactivity-probe=SECS time between inactivity probes\n"
505 " --max-idle=SECS max idle for flows set up by switch\n"
506 " --max-backoff=SECS max time between controller connection\n"
507 " attempts (default: 8 seconds)\n"
508 " -l, --listen=METHOD allow management connections on METHOD\n"
509 " (a passive OpenFlow connection method)\n"
510 " --snoop=METHOD allow controller snooping on METHOD\n"
511 " (a passive OpenFlow connection method)\n"
512 " --out-of-band controller connection is out-of-band\n"
513 " --netflow=HOST:PORT configure NetFlow output target\n"
514 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
515 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
516 " --burst-limit=BURST limit on packet credit for idle time\n");
519 printf("\nOther options:\n"
520 " -h, --help display this help message\n"
521 " -V, --version display version information\n");
522 leak_checker_usage();