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 /* Behavior when the connection to the controller fails. */
53 FAIL_OPEN, /* Act as learning switch. */
54 FAIL_CLOSED /* Drop all packets. */
57 /* Settings that may be configured by the user. */
59 /* Overall mode of operation. */
60 bool discovery; /* Discover the controller automatically? */
61 bool in_band; /* Connect to controller in-band? */
64 uint64_t datapath_id; /* Datapath ID. */
65 const char *dp_name; /* Name of local datapath. */
66 struct svec ports; /* Set of ports to add to datapath (if any). */
68 /* Description strings. */
69 const char *mfr_desc; /* Manufacturer. */
70 const char *hw_desc; /* Hardware. */
71 const char *sw_desc; /* Software version. */
72 const char *serial_desc; /* Serial number. */
74 /* Related vconns and network devices. */
75 const char *controller_name; /* Controller (if not discovery mode). */
76 struct svec listeners; /* Listen for management connections. */
77 struct svec snoops; /* Listen for controller snooping conns. */
79 /* Failure behavior. */
80 enum fail_mode fail_mode; /* Act as learning switch if no controller? */
81 int max_idle; /* Idle time for flows in fail-open mode. */
82 int probe_interval; /* # seconds idle before sending echo request. */
83 int max_backoff; /* Max # seconds between connection attempts. */
85 /* Packet-in rate-limiting. */
86 int rate_limit; /* Tokens added to bucket per second. */
87 int burst_limit; /* Maximum number token bucket size. */
89 /* Discovery behavior. */
90 const char *accept_controller_re; /* Controller vconns to accept. */
91 bool update_resolv_conf; /* Update /etc/resolv.conf? */
93 /* Spanning tree protocol. */
97 uint64_t mgmt_id; /* Management ID. */
100 struct svec netflow; /* NetFlow targets. */
103 static void parse_options(int argc, char *argv[], struct ofsettings *);
104 static void usage(void) NO_RETURN;
107 main(int argc, char *argv[])
109 struct unixctl_server *unixctl;
110 struct ofproto *ofproto;
113 struct netflow_options nf_options;
115 set_program_name(argv[0]);
118 parse_options(argc, argv, &s);
119 signal(SIGPIPE, SIG_IGN);
121 die_if_already_running();
124 /* Start listening for ovs-appctl requests. */
125 error = unixctl_server_create(NULL, &unixctl);
127 ovs_fatal(error, "Could not listen for unixctl connections");
130 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
131 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
133 /* Create the datapath and add ports to it, if requested by the user. */
139 error = dpif_create_and_open(s.dp_name, &dpif);
141 ovs_fatal(error, "could not create datapath");
144 SVEC_FOR_EACH (i, port, &s.ports) {
145 error = dpif_port_add(dpif, port, 0, NULL);
147 ovs_fatal(error, "failed to add %s as a port", port);
153 /* Start OpenFlow processing. */
154 error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
156 ovs_fatal(error, "could not initialize openflow switch");
158 error = ofproto_set_in_band(ofproto, s.in_band);
160 ovs_fatal(error, "failed to configure in-band control");
162 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
163 s.update_resolv_conf);
165 ovs_fatal(error, "failed to configure controller discovery");
168 ofproto_set_datapath_id(ofproto, s.datapath_id);
171 ofproto_set_mgmt_id(ofproto, s.mgmt_id);
173 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc, s.serial_desc);
174 if (!s.listeners.n) {
175 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
176 ovs_rundir, s.dp_name));
177 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
178 svec_clear(&s.listeners);
180 error = ofproto_set_listeners(ofproto, &s.listeners);
182 ovs_fatal(error, "failed to configure management connections");
184 error = ofproto_set_snoops(ofproto, &s.snoops);
187 "failed to configure controller snooping connections");
189 memset(&nf_options, 0, sizeof nf_options);
190 nf_options.collectors = s.netflow;
191 error = ofproto_set_netflow(ofproto, &nf_options);
193 ovs_fatal(error, "failed to configure NetFlow collectors");
195 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
196 ofproto_set_probe_interval(ofproto, s.probe_interval);
197 ofproto_set_max_backoff(ofproto, s.max_backoff);
198 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
199 error = ofproto_set_stp(ofproto, s.enable_stp);
201 ovs_fatal(error, "failed to configure STP");
204 error = ofproto_set_controller(ofproto, s.controller_name);
206 ovs_fatal(error, "failed to configure controller");
210 daemonize_complete();
212 while (ofproto_is_alive(ofproto)) {
213 error = ofproto_run(ofproto);
215 ovs_fatal(error, "unrecoverable datapath error");
217 unixctl_server_run(unixctl);
221 ofproto_wait(ofproto);
222 unixctl_server_wait(unixctl);
231 /* User interface. */
234 parse_options(int argc, char *argv[], struct ofsettings *s)
237 OPT_DATAPATH_ID = UCHAR_MAX + 1,
246 OPT_INACTIVITY_PROBE,
252 OPT_BOOTSTRAP_CA_CERT,
261 LEAK_CHECKER_OPTION_ENUMS
263 static struct option long_options[] = {
264 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
265 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
266 {"hardware", required_argument, 0, OPT_HARDWARE},
267 {"software", required_argument, 0, OPT_SOFTWARE},
268 {"serial", required_argument, 0, OPT_SERIAL},
269 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
270 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
271 {"config", required_argument, 0, 'F'},
272 {"br-name", required_argument, 0, OPT_BR_NAME},
273 {"fail", required_argument, 0, OPT_FAIL_MODE},
274 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
275 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
276 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
277 {"listen", required_argument, 0, 'l'},
278 {"snoop", required_argument, 0, OPT_SNOOP},
279 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
280 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
281 {"stp", no_argument, 0, OPT_STP},
282 {"no-stp", no_argument, 0, OPT_NO_STP},
283 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
284 {"in-band", no_argument, 0, OPT_IN_BAND},
285 {"netflow", required_argument, 0, OPT_NETFLOW},
286 {"mgmt-id", required_argument, 0, OPT_MGMT_ID},
287 {"ports", required_argument, 0, OPT_PORTS},
288 {"verbose", optional_argument, 0, 'v'},
289 {"help", no_argument, 0, 'h'},
290 {"version", no_argument, 0, 'V'},
293 LEAK_CHECKER_LONG_OPTIONS,
295 STREAM_SSL_LONG_OPTIONS
296 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
300 char *short_options = long_options_to_short_options(long_options);
302 /* Set defaults that we can figure out before parsing options. */
307 s->serial_desc = NULL;
308 svec_init(&s->listeners);
309 svec_init(&s->snoops);
310 s->fail_mode = FAIL_OPEN;
312 s->probe_interval = 0;
314 s->update_resolv_conf = true;
317 s->accept_controller_re = NULL;
318 s->enable_stp = false;
320 svec_init(&s->netflow);
322 svec_init(&s->ports);
326 c = getopt_long(argc, argv, short_options, long_options, NULL);
332 case OPT_DATAPATH_ID:
333 if (!dpid_from_string(optarg, &s->datapath_id)) {
334 ovs_fatal(0, "argument to --datapath-id must be "
335 "exactly 12 hex digits and may not be all-zero");
339 case OPT_MANUFACTURER:
340 s->mfr_desc = optarg;
352 s->serial_desc = optarg;
355 case OPT_ACCEPT_VCONN:
356 s->accept_controller_re = optarg;
359 case OPT_NO_RESOLV_CONF:
360 s->update_resolv_conf = false;
364 if (!strcmp(optarg, "open")) {
365 s->fail_mode = FAIL_OPEN;
366 } else if (!strcmp(optarg, "closed")) {
367 s->fail_mode = FAIL_CLOSED;
369 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
373 case OPT_INACTIVITY_PROBE:
374 s->probe_interval = atoi(optarg);
375 if (s->probe_interval < 5) {
376 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
381 if (!strcmp(optarg, "permanent")) {
382 s->max_idle = OFP_FLOW_PERMANENT;
384 s->max_idle = atoi(optarg);
385 if (s->max_idle < 1 || s->max_idle > 65535) {
386 ovs_fatal(0, "--max-idle argument must be between 1 and "
387 "65535 or the word 'permanent'");
392 case OPT_MAX_BACKOFF:
393 s->max_backoff = atoi(optarg);
394 if (s->max_backoff < 1) {
395 ovs_fatal(0, "--max-backoff argument must be at least 1");
396 } else if (s->max_backoff > 3600) {
397 s->max_backoff = 3600;
403 s->rate_limit = atoi(optarg);
404 if (s->rate_limit < 1) {
405 ovs_fatal(0, "--rate-limit argument must be at least 1");
408 s->rate_limit = 1000;
412 case OPT_BURST_LIMIT:
413 s->burst_limit = atoi(optarg);
414 if (s->burst_limit < 1) {
415 ovs_fatal(0, "--burst-limit argument must be at least 1");
420 s->enable_stp = true;
424 s->enable_stp = false;
427 case OPT_OUT_OF_BAND:
436 svec_add(&s->netflow, optarg);
440 if (strlen(optarg) != 12
441 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
442 ovs_fatal(0, "argument to --mgmt-id must be "
443 "exactly 12 hex digits");
445 s->mgmt_id = strtoll(optarg, NULL, 16);
447 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
452 svec_add(&s->listeners, optarg);
456 svec_add(&s->snoops, optarg);
460 svec_split(&s->ports, optarg, ",");
467 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
470 DAEMON_OPTION_HANDLERS
474 LEAK_CHECKER_OPTION_HANDLERS
477 STREAM_SSL_OPTION_HANDLERS
479 case OPT_BOOTSTRAP_CA_CERT:
480 stream_ssl_set_ca_cert_file(optarg, true);
495 if (argc < 1 || argc > 2) {
496 ovs_fatal(0, "need one or two non-option arguments; "
497 "use --help for usage");
500 /* Local and remote vconns. */
501 s->dp_name = argv[0];
502 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
504 /* Set accept_controller_regex. */
505 if (!s->accept_controller_re) {
506 s->accept_controller_re
507 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
510 /* Mode of operation. */
511 s->discovery = s->controller_name == NULL;
512 if (s->discovery && !s->in_band) {
513 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
517 if (s->rate_limit && s->rate_limit < 100) {
518 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
525 printf("%s: an OpenFlow switch implementation.\n"
526 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
527 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
528 "CONTROLLER is an active OpenFlow connection method; if it is\n"
529 "omitted, then ovs-openflowd performs controller discovery.\n",
530 program_name, program_name);
531 vconn_usage(true, true, true);
532 printf("\nOpenFlow options:\n"
533 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
534 " (ID must consist of 12 hex digits)\n"
535 " --mgmt-id=ID Use ID as the management ID\n"
536 " (ID must consist of 12 hex digits)\n"
537 " --manufacturer=MFR Identify manufacturer as MFR\n"
538 " --hardware=HW Identify hardware as HW\n"
539 " --software=SW Identify software as SW\n"
540 " --serial=SERIAL Identify serial number as SERIAL\n"
541 "\nController discovery options:\n"
542 " --accept-vconn=REGEX accept matching discovered controllers\n"
543 " --no-resolv-conf do not update /etc/resolv.conf\n"
544 "\nNetworking options:\n"
545 " --fail=open|closed when controller connection fails:\n"
546 " closed: drop all packets\n"
547 " open (default): act as learning switch\n"
548 " --inactivity-probe=SECS time between inactivity probes\n"
549 " --max-idle=SECS max idle for flows set up by switch\n"
550 " --max-backoff=SECS max time between controller connection\n"
551 " attempts (default: 8 seconds)\n"
552 " -l, --listen=METHOD allow management connections on METHOD\n"
553 " (a passive OpenFlow connection method)\n"
554 " --snoop=METHOD allow controller snooping on METHOD\n"
555 " (a passive OpenFlow connection method)\n"
556 " --out-of-band controller connection is out-of-band\n"
557 " --netflow=HOST:PORT configure NetFlow output target\n"
558 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
559 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
560 " --burst-limit=BURST limit on packet credit for idle time\n");
563 printf("\nOther options:\n"
564 " -h, --help display this help message\n"
565 " -V, --version display version information\n");
566 leak_checker_usage();