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 #define THIS_MODULE VLM_openflowd
51 /* Settings that may be configured by the user. */
53 /* Controller configuration. */
54 struct ofproto_controller controller;
57 uint64_t datapath_id; /* Datapath ID. */
58 char *dp_name; /* Name of local datapath. */
59 char *dp_type; /* Type of local datapath. */
60 struct svec ports; /* Set of ports to add to datapath (if any). */
62 /* Description strings. */
63 const char *mfr_desc; /* Manufacturer. */
64 const char *hw_desc; /* Hardware. */
65 const char *sw_desc; /* Software version. */
66 const char *serial_desc; /* Serial number. */
67 const char *dp_desc; /* Datapath description. */
69 /* Related vconns and network devices. */
70 struct svec listeners; /* Listen for management connections. */
71 struct svec snoops; /* Listen for controller snooping conns. */
73 /* Failure behavior. */
74 int max_idle; /* Idle time for flows in fail-open mode. */
76 /* Spanning tree protocol. */
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]);
100 parse_options(argc, argv, &s);
101 signal(SIGPIPE, SIG_IGN);
103 die_if_already_running();
106 /* Start listening for ovs-appctl requests. */
107 error = unixctl_server_create(NULL, &unixctl);
112 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
113 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
115 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
117 ovs_fatal(error, "could not create datapath");
120 /* Add ports to the datapath if requested by the user. */
124 struct netdev *netdev;
126 SVEC_FOR_EACH (i, port, &s.ports) {
127 error = netdev_open_default(port, &netdev);
129 ovs_fatal(error, "failed to open %s as a device", port);
132 error = dpif_port_add(dpif, port, 0, NULL);
134 ovs_fatal(error, "failed to add %s as a port", port);
139 /* Start OpenFlow processing. */
140 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
142 ovs_fatal(error, "could not initialize openflow switch");
145 ofproto_set_datapath_id(ofproto, s.datapath_id);
147 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
148 s.serial_desc, s.dp_desc);
149 if (!s.listeners.n) {
150 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
151 ovs_rundir, s.dp_name));
152 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
153 svec_clear(&s.listeners);
155 error = ofproto_set_listeners(ofproto, &s.listeners);
157 ovs_fatal(error, "failed to configure management connections");
159 error = ofproto_set_snoops(ofproto, &s.snoops);
162 "failed to configure controller snooping connections");
164 memset(&nf_options, 0, sizeof nf_options);
165 nf_options.collectors = s.netflow;
166 error = ofproto_set_netflow(ofproto, &nf_options);
168 ovs_fatal(error, "failed to configure NetFlow collectors");
170 error = ofproto_set_stp(ofproto, s.enable_stp);
172 ovs_fatal(error, "failed to configure STP");
174 ofproto_set_controller(ofproto, &s.controller);
176 daemonize_complete();
178 while (ofproto_is_alive(ofproto)) {
179 error = ofproto_run(ofproto);
181 ovs_fatal(error, "unrecoverable datapath error");
183 unixctl_server_run(unixctl);
187 ofproto_wait(ofproto);
188 unixctl_server_wait(unixctl);
199 /* User interface. */
202 parse_options(int argc, char *argv[], struct ofsettings *s)
205 OPT_DATAPATH_ID = UCHAR_MAX + 1,
215 OPT_INACTIVITY_PROBE,
221 OPT_BOOTSTRAP_CA_CERT,
229 LEAK_CHECKER_OPTION_ENUMS
231 static struct option long_options[] = {
232 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
233 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
234 {"hw-desc", required_argument, 0, OPT_HW_DESC},
235 {"sw-desc", required_argument, 0, OPT_SW_DESC},
236 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
237 {"dp-desc", required_argument, 0, OPT_DP_DESC},
238 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
239 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
240 {"config", required_argument, 0, 'F'},
241 {"br-name", required_argument, 0, OPT_BR_NAME},
242 {"fail", required_argument, 0, OPT_FAIL_MODE},
243 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
244 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
245 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
246 {"listen", required_argument, 0, 'l'},
247 {"snoop", required_argument, 0, OPT_SNOOP},
248 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
249 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
250 {"stp", no_argument, 0, OPT_STP},
251 {"no-stp", no_argument, 0, OPT_NO_STP},
252 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
253 {"in-band", no_argument, 0, OPT_IN_BAND},
254 {"netflow", required_argument, 0, OPT_NETFLOW},
255 {"ports", required_argument, 0, OPT_PORTS},
256 {"verbose", optional_argument, 0, 'v'},
257 {"help", no_argument, 0, 'h'},
258 {"version", no_argument, 0, 'V'},
261 LEAK_CHECKER_LONG_OPTIONS,
263 STREAM_SSL_LONG_OPTIONS
264 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
268 char *short_options = long_options_to_short_options(long_options);
270 /* Set defaults that we can figure out before parsing options. */
271 s->controller.max_backoff = 8;
272 s->controller.probe_interval = 5;
273 s->controller.fail = OFPROTO_FAIL_STANDALONE;
274 s->controller.band = OFPROTO_IN_BAND;
275 s->controller.accept_re = NULL;
276 s->controller.update_resolv_conf = true;
277 s->controller.rate_limit = 0;
278 s->controller.burst_limit = 0;
283 s->serial_desc = NULL;
285 svec_init(&s->listeners);
286 svec_init(&s->snoops);
288 s->enable_stp = false;
289 svec_init(&s->netflow);
290 svec_init(&s->ports);
294 c = getopt_long(argc, argv, short_options, long_options, NULL);
300 case OPT_DATAPATH_ID:
301 if (!dpid_from_string(optarg, &s->datapath_id)) {
302 ovs_fatal(0, "argument to --datapath-id must be "
303 "exactly 16 hex digits and may not be all-zero");
308 s->mfr_desc = optarg;
319 case OPT_SERIAL_DESC:
320 s->serial_desc = optarg;
327 case OPT_ACCEPT_VCONN:
328 s->controller.accept_re = optarg;
331 case OPT_NO_RESOLV_CONF:
332 s->controller.update_resolv_conf = false;
336 if (!strcmp(optarg, "open")) {
337 s->controller.fail = OFPROTO_FAIL_STANDALONE;
338 } else if (!strcmp(optarg, "closed")) {
339 s->controller.fail = OFPROTO_FAIL_SECURE;
341 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
345 case OPT_INACTIVITY_PROBE:
346 s->controller.probe_interval = atoi(optarg);
347 if (s->controller.probe_interval < 5) {
348 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
353 if (!strcmp(optarg, "permanent")) {
354 s->max_idle = OFP_FLOW_PERMANENT;
356 s->max_idle = atoi(optarg);
357 if (s->max_idle < 1 || s->max_idle > 65535) {
358 ovs_fatal(0, "--max-idle argument must be between 1 and "
359 "65535 or the word 'permanent'");
364 case OPT_MAX_BACKOFF:
365 s->controller.max_backoff = atoi(optarg);
366 if (s->controller.max_backoff < 1) {
367 ovs_fatal(0, "--max-backoff argument must be at least 1");
368 } else if (s->controller.max_backoff > 3600) {
369 s->controller.max_backoff = 3600;
375 s->controller.rate_limit = atoi(optarg);
376 if (s->controller.rate_limit < 1) {
377 ovs_fatal(0, "--rate-limit argument must be at least 1");
380 s->controller.rate_limit = 1000;
384 case OPT_BURST_LIMIT:
385 s->controller.burst_limit = atoi(optarg);
386 if (s->controller.burst_limit < 1) {
387 ovs_fatal(0, "--burst-limit argument must be at least 1");
392 s->enable_stp = true;
396 s->enable_stp = false;
399 case OPT_OUT_OF_BAND:
400 s->controller.band = OFPROTO_OUT_OF_BAND;
404 s->controller.band = OFPROTO_IN_BAND;
408 svec_add(&s->netflow, optarg);
412 svec_add(&s->listeners, optarg);
416 svec_add(&s->snoops, optarg);
420 svec_split(&s->ports, optarg, ",");
427 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
430 DAEMON_OPTION_HANDLERS
434 LEAK_CHECKER_OPTION_HANDLERS
437 STREAM_SSL_OPTION_HANDLERS
439 case OPT_BOOTSTRAP_CA_CERT:
440 stream_ssl_set_ca_cert_file(optarg, true);
455 if (argc < 1 || argc > 2) {
456 ovs_fatal(0, "need one or two non-option arguments; "
457 "use --help for usage");
460 /* Local and remote vconns. */
461 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
463 s->controller.target = argc > 1 ? argv[1] : "discover";
464 if (!strcmp(s->controller.target, "discover")
465 && s->controller.band == OFPROTO_OUT_OF_BAND) {
466 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
469 /* Set accept_controller_regex. */
470 if (!s->controller.accept_re) {
471 s->controller.accept_re
472 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
476 if (s->controller.rate_limit && s->controller.rate_limit < 100) {
477 VLOG_WARN("Rate limit set to unusually low value %d",
478 s->controller.rate_limit);
485 printf("%s: an OpenFlow switch implementation.\n"
486 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
487 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
488 "CONTROLLER is an active OpenFlow connection method; if it is\n"
489 "omitted, then ovs-openflowd performs controller discovery.\n",
490 program_name, program_name);
491 vconn_usage(true, true, true);
492 printf("\nOpenFlow options:\n"
493 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
494 " (ID must consist of 16 hex digits)\n"
495 " --mfr-desc=MFR Identify manufacturer as MFR\n"
496 " --hw-desc=HW Identify hardware as HW\n"
497 " --sw-desc=SW Identify software as SW\n"
498 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
499 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
500 "\nController discovery options:\n"
501 " --accept-vconn=REGEX accept matching discovered controllers\n"
502 " --no-resolv-conf do not update /etc/resolv.conf\n"
503 "\nNetworking options:\n"
504 " --fail=open|closed when controller connection fails:\n"
505 " closed: drop all packets\n"
506 " open (default): act as learning switch\n"
507 " --inactivity-probe=SECS time between inactivity probes\n"
508 " --max-idle=SECS max idle for flows set up by switch\n"
509 " --max-backoff=SECS max time between controller connection\n"
510 " attempts (default: 8 seconds)\n"
511 " -l, --listen=METHOD allow management connections on METHOD\n"
512 " (a passive OpenFlow connection method)\n"
513 " --snoop=METHOD allow controller snooping on METHOD\n"
514 " (a passive OpenFlow connection method)\n"
515 " --out-of-band controller connection is out-of-band\n"
516 " --netflow=HOST:PORT configure NetFlow output target\n"
517 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
518 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
519 " --burst-limit=BURST limit on packet credit for idle time\n");
522 printf("\nOther options:\n"
523 " -h, --help display this help message\n"
524 " -V, --version display version information\n");
525 leak_checker_usage();