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"
46 #include "vconn-ssl.h"
50 #define THIS_MODULE VLM_openflowd
52 /* Behavior when the connection to the controller fails. */
54 FAIL_OPEN, /* Act as learning switch. */
55 FAIL_CLOSED /* Drop all packets. */
58 /* Settings that may be configured by the user. */
60 /* Overall mode of operation. */
61 bool discovery; /* Discover the controller automatically? */
62 bool in_band; /* Connect to controller in-band? */
65 uint64_t datapath_id; /* Datapath ID. */
66 const char *dp_name; /* Name of local datapath. */
67 struct svec ports; /* Set of ports to add to datapath (if any). */
69 /* Description strings. */
70 const char *mfr_desc; /* Manufacturer. */
71 const char *hw_desc; /* Hardware. */
72 const char *sw_desc; /* Software version. */
73 const char *serial_desc; /* Serial number. */
74 const char *dp_desc; /* Serial number. */
76 /* Related vconns and network devices. */
77 const char *controller_name; /* Controller (if not discovery mode). */
78 struct svec listeners; /* Listen for management connections. */
79 struct svec snoops; /* Listen for controller snooping conns. */
81 /* Failure behavior. */
82 enum fail_mode fail_mode; /* Act as learning switch if no controller? */
83 int max_idle; /* Idle time for flows in fail-open mode. */
84 int probe_interval; /* # seconds idle before sending echo request. */
85 int max_backoff; /* Max # seconds between connection attempts. */
87 /* Packet-in rate-limiting. */
88 int rate_limit; /* Tokens added to bucket per second. */
89 int burst_limit; /* Maximum number token bucket size. */
91 /* Discovery behavior. */
92 const char *accept_controller_re; /* Controller vconns to accept. */
93 bool update_resolv_conf; /* Update /etc/resolv.conf? */
95 /* Spanning tree protocol. */
98 /* Remote command execution. */
99 char *command_acl; /* Command white/blacklist, as shell globs. */
100 char *command_dir; /* Directory that contains commands. */
103 uint64_t mgmt_id; /* Management ID. */
106 struct svec netflow; /* NetFlow targets. */
109 static void parse_options(int argc, char *argv[], struct ofsettings *);
110 static void usage(void) NO_RETURN;
113 main(int argc, char *argv[])
115 struct unixctl_server *unixctl;
116 struct ofproto *ofproto;
119 struct netflow_options nf_options;
121 set_program_name(argv[0]);
122 register_fault_handlers();
125 parse_options(argc, argv, &s);
126 signal(SIGPIPE, SIG_IGN);
128 die_if_already_running();
131 /* Start listening for ovs-appctl requests. */
132 error = unixctl_server_create(NULL, &unixctl);
134 ovs_fatal(error, "Could not listen for unixctl connections");
137 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
138 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
140 /* Create the datapath and add ports to it, if requested by the user. */
146 error = dpif_create_and_open(s.dp_name, &dpif);
148 ovs_fatal(error, "could not create datapath");
151 SVEC_FOR_EACH (i, port, &s.ports) {
152 error = dpif_port_add(dpif, port, 0, NULL);
154 ovs_fatal(error, "failed to add %s as a port", port);
160 /* Start OpenFlow processing. */
161 error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
163 ovs_fatal(error, "could not initialize openflow switch");
165 error = ofproto_set_in_band(ofproto, s.in_band);
167 ovs_fatal(error, "failed to configure in-band control");
169 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
170 s.update_resolv_conf);
172 ovs_fatal(error, "failed to configure controller discovery");
175 ofproto_set_datapath_id(ofproto, s.datapath_id);
178 ofproto_set_mgmt_id(ofproto, s.mgmt_id);
180 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
181 s.serial_desc, s.dp_desc);
182 if (!s.listeners.n) {
183 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
184 ovs_rundir, s.dp_name));
185 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
186 svec_clear(&s.listeners);
188 error = ofproto_set_listeners(ofproto, &s.listeners);
190 ovs_fatal(error, "failed to configure management connections");
192 error = ofproto_set_snoops(ofproto, &s.snoops);
195 "failed to configure controller snooping connections");
197 memset(&nf_options, 0, sizeof nf_options);
198 nf_options.collectors = s.netflow;
199 error = ofproto_set_netflow(ofproto, &nf_options);
201 ovs_fatal(error, "failed to configure NetFlow collectors");
203 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
204 ofproto_set_probe_interval(ofproto, s.probe_interval);
205 ofproto_set_max_backoff(ofproto, s.max_backoff);
206 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
207 error = ofproto_set_stp(ofproto, s.enable_stp);
209 ovs_fatal(error, "failed to configure STP");
211 error = ofproto_set_remote_execution(ofproto, s.command_acl,
214 ovs_fatal(error, "failed to configure remote command execution");
217 error = ofproto_set_controller(ofproto, s.controller_name);
219 ovs_fatal(error, "failed to configure controller");
223 while (ofproto_is_alive(ofproto)) {
224 error = ofproto_run(ofproto);
226 ovs_fatal(error, "unrecoverable datapath error");
228 unixctl_server_run(unixctl);
232 ofproto_wait(ofproto);
233 unixctl_server_wait(unixctl);
242 /* User interface. */
245 parse_options(int argc, char *argv[], struct ofsettings *s)
248 OPT_DATAPATH_ID = UCHAR_MAX + 1,
258 OPT_INACTIVITY_PROBE,
264 OPT_BOOTSTRAP_CA_CERT,
275 LEAK_CHECKER_OPTION_ENUMS
277 static struct option long_options[] = {
278 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
279 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
280 {"hardware", required_argument, 0, OPT_HARDWARE},
281 {"software", required_argument, 0, OPT_SOFTWARE},
282 {"serial", required_argument, 0, OPT_SERIAL},
283 {"dp_desc", required_argument, 0, OPT_DP_DESC},
284 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
285 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
286 {"config", required_argument, 0, 'F'},
287 {"br-name", required_argument, 0, OPT_BR_NAME},
288 {"fail", required_argument, 0, OPT_FAIL_MODE},
289 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
290 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
291 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
292 {"listen", required_argument, 0, 'l'},
293 {"snoop", required_argument, 0, OPT_SNOOP},
294 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
295 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
296 {"stp", no_argument, 0, OPT_STP},
297 {"no-stp", no_argument, 0, OPT_NO_STP},
298 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
299 {"in-band", no_argument, 0, OPT_IN_BAND},
300 {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
301 {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
302 {"netflow", required_argument, 0, OPT_NETFLOW},
303 {"mgmt-id", required_argument, 0, OPT_MGMT_ID},
304 {"ports", required_argument, 0, OPT_PORTS},
305 {"verbose", optional_argument, 0, 'v'},
306 {"help", no_argument, 0, 'h'},
307 {"version", no_argument, 0, 'V'},
310 LEAK_CHECKER_LONG_OPTIONS,
312 VCONN_SSL_LONG_OPTIONS
313 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
317 char *short_options = long_options_to_short_options(long_options);
319 /* Set defaults that we can figure out before parsing options. */
324 s->serial_desc = NULL;
326 svec_init(&s->listeners);
327 svec_init(&s->snoops);
328 s->fail_mode = FAIL_OPEN;
330 s->probe_interval = 0;
332 s->update_resolv_conf = true;
335 s->accept_controller_re = NULL;
336 s->enable_stp = false;
339 s->command_dir = NULL;
340 svec_init(&s->netflow);
342 svec_init(&s->ports);
346 c = getopt_long(argc, argv, short_options, long_options, NULL);
352 case OPT_DATAPATH_ID:
353 if (strlen(optarg) != 16
354 || strspn(optarg, "0123456789abcdefABCDEF") != 16) {
355 ovs_fatal(0, "argument to --datapath-id must be "
356 "exactly 16 hex digits");
358 s->datapath_id = strtoll(optarg, NULL, 16);
359 if (!s->datapath_id) {
360 ovs_fatal(0, "argument to --datapath-id must be nonzero");
364 case OPT_MANUFACTURER:
365 s->mfr_desc = optarg;
377 s->serial_desc = optarg;
384 case OPT_ACCEPT_VCONN:
385 s->accept_controller_re = optarg;
388 case OPT_NO_RESOLV_CONF:
389 s->update_resolv_conf = false;
393 if (!strcmp(optarg, "open")) {
394 s->fail_mode = FAIL_OPEN;
395 } else if (!strcmp(optarg, "closed")) {
396 s->fail_mode = FAIL_CLOSED;
398 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
402 case OPT_INACTIVITY_PROBE:
403 s->probe_interval = atoi(optarg);
404 if (s->probe_interval < 5) {
405 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
410 if (!strcmp(optarg, "permanent")) {
411 s->max_idle = OFP_FLOW_PERMANENT;
413 s->max_idle = atoi(optarg);
414 if (s->max_idle < 1 || s->max_idle > 65535) {
415 ovs_fatal(0, "--max-idle argument must be between 1 and "
416 "65535 or the word 'permanent'");
421 case OPT_MAX_BACKOFF:
422 s->max_backoff = atoi(optarg);
423 if (s->max_backoff < 1) {
424 ovs_fatal(0, "--max-backoff argument must be at least 1");
425 } else if (s->max_backoff > 3600) {
426 s->max_backoff = 3600;
432 s->rate_limit = atoi(optarg);
433 if (s->rate_limit < 1) {
434 ovs_fatal(0, "--rate-limit argument must be at least 1");
437 s->rate_limit = 1000;
441 case OPT_BURST_LIMIT:
442 s->burst_limit = atoi(optarg);
443 if (s->burst_limit < 1) {
444 ovs_fatal(0, "--burst-limit argument must be at least 1");
449 s->enable_stp = true;
453 s->enable_stp = false;
456 case OPT_OUT_OF_BAND:
464 case OPT_COMMAND_ACL:
465 s->command_acl = (s->command_acl[0]
466 ? xasprintf("%s,%s", s->command_acl, optarg)
470 case OPT_COMMAND_DIR:
471 s->command_dir = optarg;
475 svec_add(&s->netflow, optarg);
479 if (strlen(optarg) != 16
480 || strspn(optarg, "0123456789abcdefABCDEF") != 16) {
481 ovs_fatal(0, "argument to --mgmt-id must be "
482 "exactly 16 hex digits");
484 s->mgmt_id = strtoll(optarg, NULL, 16);
486 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
491 svec_add(&s->listeners, optarg);
495 svec_add(&s->snoops, optarg);
499 svec_split(&s->ports, optarg, ",");
506 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
509 DAEMON_OPTION_HANDLERS
513 LEAK_CHECKER_OPTION_HANDLERS
516 VCONN_SSL_OPTION_HANDLERS
518 case OPT_BOOTSTRAP_CA_CERT:
519 vconn_ssl_set_ca_cert_file(optarg, true);
534 if (argc < 1 || argc > 2) {
535 ovs_fatal(0, "need one or two non-option arguments; "
536 "use --help for usage");
539 /* Local and remote vconns. */
540 s->dp_name = argv[0];
541 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
543 /* Set accept_controller_regex. */
544 if (!s->accept_controller_re) {
545 s->accept_controller_re
546 = vconn_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
549 /* Mode of operation. */
550 s->discovery = s->controller_name == NULL;
551 if (s->discovery && !s->in_band) {
552 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
556 if (s->rate_limit && s->rate_limit < 100) {
557 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
564 printf("%s: an OpenFlow switch implementation.\n"
565 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
566 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
567 "CONTROLLER is an active OpenFlow connection method; if it is\n"
568 "omitted, then ovs-openflowd performs controller discovery.\n",
569 program_name, program_name);
570 vconn_usage(true, true, true);
571 printf("\nOpenFlow options:\n"
572 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
573 " (ID must consist of 16 hex digits)\n"
574 " --mgmt-id=ID Use ID as the management ID\n"
575 " (ID must consist of 16 hex digits)\n"
576 " --manufacturer=MFR Identify manufacturer as MFR\n"
577 " --hardware=HW Identify hardware as HW\n"
578 " --software=SW Identify software as SW\n"
579 " --serial=SERIAL Identify serial number as SERIAL\n"
580 " --dp_desc=DP_DESC Identify dp description as DP_DESC\n"
581 "\nController discovery options:\n"
582 " --accept-vconn=REGEX accept matching discovered controllers\n"
583 " --no-resolv-conf do not update /etc/resolv.conf\n"
584 "\nNetworking options:\n"
585 " --fail=open|closed when controller connection fails:\n"
586 " closed: drop all packets\n"
587 " open (default): act as learning switch\n"
588 " --inactivity-probe=SECS time between inactivity probes\n"
589 " --max-idle=SECS max idle for flows set up by switch\n"
590 " --max-backoff=SECS max time between controller connection\n"
591 " attempts (default: 8 seconds)\n"
592 " -l, --listen=METHOD allow management connections on METHOD\n"
593 " (a passive OpenFlow connection method)\n"
594 " --snoop=METHOD allow controller snooping on METHOD\n"
595 " (a passive OpenFlow connection method)\n"
596 " --out-of-band controller connection is out-of-band\n"
597 " --netflow=HOST:PORT configure NetFlow output target\n"
598 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
599 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
600 " --burst-limit=BURST limit on packet credit for idle time\n"
601 "\nRemote command execution options:\n"
602 " --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
603 " --command-dir=DIR set command dir (default: %s/commands)\n",
607 printf("\nOther options:\n"
608 " -h, --help display this help message\n"
609 " -V, --version display version information\n");
610 leak_checker_usage();