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"
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;
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 svec 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 svec snoops; /* Listen for controller snooping conns. */
77 /* Failure behavior. */
78 int max_idle; /* Idle time for flows in fail-open mode. */
81 struct svec netflow; /* NetFlow targets. */
84 static void parse_options(int argc, char *argv[], struct ofsettings *);
85 static void usage(void) NO_RETURN;
88 main(int argc, char *argv[])
90 struct unixctl_server *unixctl;
91 struct ofproto *ofproto;
95 struct netflow_options nf_options;
97 proctitle_init(argc, argv);
98 set_program_name(argv[0]);
99 parse_options(argc, argv, &s);
100 signal(SIGPIPE, SIG_IGN);
102 die_if_already_running();
105 /* Start listening for ovs-appctl requests. */
106 error = unixctl_server_create(s.unixctl_path, &unixctl);
111 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
112 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
114 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
116 ovs_fatal(error, "could not create datapath");
119 /* Add ports to the datapath if requested by the user. */
124 SVEC_FOR_EACH (i, port, &s.ports) {
125 error = dpif_port_add(dpif, port, 0, NULL);
127 ovs_fatal(error, "failed to add %s as a port", port);
132 /* Start OpenFlow processing. */
133 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
135 ovs_fatal(error, "could not initialize openflow switch");
138 ofproto_set_datapath_id(ofproto, s.datapath_id);
140 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
141 s.serial_desc, s.dp_desc);
142 error = ofproto_set_snoops(ofproto, &s.snoops);
145 "failed to configure controller snooping connections");
147 memset(&nf_options, 0, sizeof nf_options);
148 nf_options.collectors = s.netflow;
149 error = ofproto_set_netflow(ofproto, &nf_options);
151 ovs_fatal(error, "failed to configure NetFlow collectors");
153 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
154 ofproto_set_fail_mode(ofproto, s.fail_mode);
156 daemonize_complete();
158 while (ofproto_is_alive(ofproto)) {
159 error = ofproto_run(ofproto);
161 ovs_fatal(error, "unrecoverable datapath error");
163 unixctl_server_run(unixctl);
167 ofproto_wait(ofproto);
168 unixctl_server_wait(unixctl);
179 /* User interface. */
182 parse_options(int argc, char *argv[], struct ofsettings *s)
185 OPT_DATAPATH_ID = UCHAR_MAX + 1,
195 OPT_INACTIVITY_PROBE,
201 OPT_BOOTSTRAP_CA_CERT,
209 LEAK_CHECKER_OPTION_ENUMS
211 static struct option long_options[] = {
212 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
213 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
214 {"hw-desc", required_argument, 0, OPT_HW_DESC},
215 {"sw-desc", required_argument, 0, OPT_SW_DESC},
216 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
217 {"dp-desc", required_argument, 0, OPT_DP_DESC},
218 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
219 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
220 {"config", required_argument, 0, 'F'},
221 {"br-name", required_argument, 0, OPT_BR_NAME},
222 {"fail", required_argument, 0, OPT_FAIL_MODE},
223 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
224 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
225 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
226 {"listen", required_argument, 0, 'l'},
227 {"snoop", required_argument, 0, OPT_SNOOP},
228 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
229 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
230 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
231 {"in-band", no_argument, 0, OPT_IN_BAND},
232 {"netflow", required_argument, 0, OPT_NETFLOW},
233 {"ports", required_argument, 0, OPT_PORTS},
234 {"unixctl", required_argument, 0, OPT_UNIXCTL},
235 {"enable-dummy", no_argument, 0, OPT_ENABLE_DUMMY},
236 {"verbose", optional_argument, 0, 'v'},
237 {"help", no_argument, 0, 'h'},
238 {"version", no_argument, 0, 'V'},
241 LEAK_CHECKER_LONG_OPTIONS,
243 STREAM_SSL_LONG_OPTIONS
244 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
248 char *short_options = long_options_to_short_options(long_options);
249 struct ofproto_controller controller_opts;
250 struct svec controllers;
253 /* Set defaults that we can figure out before parsing options. */
254 controller_opts.target = NULL;
255 controller_opts.max_backoff = 8;
256 controller_opts.probe_interval = 5;
257 controller_opts.band = OFPROTO_IN_BAND;
258 controller_opts.accept_re = NULL;
259 controller_opts.update_resolv_conf = true;
260 controller_opts.rate_limit = 0;
261 controller_opts.burst_limit = 0;
262 s->unixctl_path = NULL;
263 s->fail_mode = OFPROTO_FAIL_STANDALONE;
268 s->serial_desc = NULL;
270 svec_init(&controllers);
271 svec_init(&s->snoops);
273 svec_init(&s->netflow);
274 svec_init(&s->ports);
278 c = getopt_long(argc, argv, short_options, long_options, NULL);
284 case OPT_DATAPATH_ID:
285 if (!dpid_from_string(optarg, &s->datapath_id)) {
286 ovs_fatal(0, "argument to --datapath-id must be "
287 "exactly 16 hex digits and may not be all-zero");
292 s->mfr_desc = optarg;
303 case OPT_SERIAL_DESC:
304 s->serial_desc = optarg;
311 case OPT_ACCEPT_VCONN:
312 controller_opts.accept_re = optarg;
315 case OPT_NO_RESOLV_CONF:
316 controller_opts.update_resolv_conf = false;
320 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
321 s->fail_mode = OFPROTO_FAIL_STANDALONE;
322 } else if (!strcmp(optarg, "closed")
323 || !strcmp(optarg, "secure")) {
324 s->fail_mode = OFPROTO_FAIL_SECURE;
326 ovs_fatal(0, "--fail argument must be \"standalone\" "
331 case OPT_INACTIVITY_PROBE:
332 controller_opts.probe_interval = atoi(optarg);
333 if (controller_opts.probe_interval < 5) {
334 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
339 if (!strcmp(optarg, "permanent")) {
340 s->max_idle = OFP_FLOW_PERMANENT;
342 s->max_idle = atoi(optarg);
343 if (s->max_idle < 1 || s->max_idle > 65535) {
344 ovs_fatal(0, "--max-idle argument must be between 1 and "
345 "65535 or the word 'permanent'");
350 case OPT_MAX_BACKOFF:
351 controller_opts.max_backoff = atoi(optarg);
352 if (controller_opts.max_backoff < 1) {
353 ovs_fatal(0, "--max-backoff argument must be at least 1");
354 } else if (controller_opts.max_backoff > 3600) {
355 controller_opts.max_backoff = 3600;
361 controller_opts.rate_limit = atoi(optarg);
362 if (controller_opts.rate_limit < 1) {
363 ovs_fatal(0, "--rate-limit argument must be at least 1");
366 controller_opts.rate_limit = 1000;
370 case OPT_BURST_LIMIT:
371 controller_opts.burst_limit = atoi(optarg);
372 if (controller_opts.burst_limit < 1) {
373 ovs_fatal(0, "--burst-limit argument must be at least 1");
377 case OPT_OUT_OF_BAND:
378 controller_opts.band = OFPROTO_OUT_OF_BAND;
382 controller_opts.band = OFPROTO_IN_BAND;
386 svec_add(&s->netflow, optarg);
390 svec_add(&controllers, optarg);
394 svec_add(&s->snoops, optarg);
398 svec_split(&s->ports, optarg, ",");
402 s->unixctl_path = optarg;
405 case OPT_ENABLE_DUMMY:
413 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
416 DAEMON_OPTION_HANDLERS
420 LEAK_CHECKER_OPTION_HANDLERS
423 STREAM_SSL_OPTION_HANDLERS
425 case OPT_BOOTSTRAP_CA_CERT:
426 stream_ssl_set_ca_cert_file(optarg, true);
442 ovs_fatal(0, "need at least one non-option arguments; "
443 "use --help for usage");
446 /* Set accept_controller_regex. */
447 if (!controller_opts.accept_re) {
448 controller_opts.accept_re
449 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
453 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
454 VLOG_WARN("Rate limit set to unusually low value %d",
455 controller_opts.rate_limit);
459 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
461 /* Figure out controller names. */
462 if (!controllers.n) {
463 svec_add_nocopy(&controllers, xasprintf("punix:%s/%s.mgmt",
464 ovs_rundir(), s->dp_name));
466 for (i = 1; i < argc; i++) {
467 svec_add(&controllers, argv[i]);
470 svec_add(&controllers, "discover");
473 /* Set up controllers. */
474 s->n_controllers = controllers.n;
475 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
476 for (i = 0; i < s->n_controllers; i++) {
477 s->controllers[i] = controller_opts;
478 s->controllers[i].target = controllers.names[i];
482 if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
483 for (i = 0; i < s->n_controllers; i++) {
484 if (!strcmp(s->controllers[i].target, "discover")) {
485 ovs_fatal(0, "Cannot perform discovery with out-of-band "
495 printf("%s: an OpenFlow switch implementation.\n"
496 "usage: %s [OPTIONS] DATAPATH [CONTROLLER...]\n"
497 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
498 "Each CONTROLLER is an active OpenFlow connection method. If\n"
499 "none is given, ovs-openflowd performs controller discovery.\n",
500 program_name, program_name);
501 vconn_usage(true, true, true);
502 printf("\nOpenFlow options:\n"
503 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
504 " (ID must consist of 16 hex digits)\n"
505 " --mfr-desc=MFR Identify manufacturer as MFR\n"
506 " --hw-desc=HW Identify hardware as HW\n"
507 " --sw-desc=SW Identify software as SW\n"
508 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
509 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
510 "\nController discovery options:\n"
511 " --accept-vconn=REGEX accept matching discovered controllers\n"
512 " --no-resolv-conf do not update /etc/resolv.conf\n"
513 "\nNetworking options:\n"
514 " --fail=open|closed when controller connection fails:\n"
515 " closed: drop all packets\n"
516 " open (default): act as learning switch\n"
517 " --inactivity-probe=SECS time between inactivity probes\n"
518 " --max-idle=SECS max idle for flows set up by switch\n"
519 " --max-backoff=SECS max time between controller connection\n"
520 " attempts (default: 8 seconds)\n"
521 " -l, --listen=METHOD allow management connections on METHOD\n"
522 " (a passive OpenFlow connection method)\n"
523 " --snoop=METHOD allow controller snooping on METHOD\n"
524 " (a passive OpenFlow connection method)\n"
525 " --out-of-band controller connection is out-of-band\n"
526 " --netflow=HOST:PORT configure NetFlow output target\n"
527 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
528 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
529 " --burst-limit=BURST limit on packet credit for idle time\n");
532 printf("\nOther options:\n"
533 " --unixctl=SOCKET override default control socket name\n"
534 " -h, --help display this help message\n"
535 " -V, --version display version information\n");
536 leak_checker_usage();