2 * Copyright (c) 2008, 2009 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. */
75 /* Related vconns and network devices. */
76 const char *controller_name; /* Controller (if not discovery mode). */
77 struct svec listeners; /* Listen for management connections. */
78 struct svec snoops; /* Listen for controller snooping conns. */
80 /* Failure behavior. */
81 enum fail_mode fail_mode; /* Act as learning switch if no controller? */
82 int max_idle; /* Idle time for flows in fail-open mode. */
83 int probe_interval; /* # seconds idle before sending echo request. */
84 int max_backoff; /* Max # seconds between connection attempts. */
86 /* Packet-in rate-limiting. */
87 int rate_limit; /* Tokens added to bucket per second. */
88 int burst_limit; /* Maximum number token bucket size. */
90 /* Discovery behavior. */
91 const char *accept_controller_re; /* Controller vconns to accept. */
92 bool update_resolv_conf; /* Update /etc/resolv.conf? */
94 /* Spanning tree protocol. */
97 /* Remote command execution. */
98 char *command_acl; /* Command white/blacklist, as shell globs. */
99 char *command_dir; /* Directory that contains commands. */
102 uint64_t mgmt_id; /* Management ID. */
105 struct svec netflow; /* NetFlow targets. */
108 static void parse_options(int argc, char *argv[], struct ofsettings *);
109 static void usage(void) NO_RETURN;
112 main(int argc, char *argv[])
114 struct unixctl_server *unixctl;
115 struct ofproto *ofproto;
118 struct netflow_options nf_options;
120 set_program_name(argv[0]);
121 register_fault_handlers();
124 parse_options(argc, argv, &s);
125 signal(SIGPIPE, SIG_IGN);
127 die_if_already_running();
130 /* Start listening for ovs-appctl requests. */
131 error = unixctl_server_create(NULL, &unixctl);
133 ovs_fatal(error, "Could not listen for unixctl connections");
136 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
137 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
139 /* Create the datapath and add ports to it, if requested by the user. */
145 error = dpif_create_and_open(s.dp_name, &dpif);
147 ovs_fatal(error, "could not create datapath");
150 SVEC_FOR_EACH (i, port, &s.ports) {
151 error = dpif_port_add(dpif, port, 0, NULL);
153 ovs_fatal(error, "failed to add %s as a port", port);
159 /* Start OpenFlow processing. */
160 error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
162 ovs_fatal(error, "could not initialize openflow switch");
164 error = ofproto_set_in_band(ofproto, s.in_band);
166 ovs_fatal(error, "failed to configure in-band control");
168 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
169 s.update_resolv_conf);
171 ovs_fatal(error, "failed to configure controller discovery");
174 ofproto_set_datapath_id(ofproto, s.datapath_id);
177 ofproto_set_mgmt_id(ofproto, s.mgmt_id);
179 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc, s.serial_desc);
180 if (!s.listeners.n) {
181 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
182 ovs_rundir, s.dp_name));
183 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
184 svec_clear(&s.listeners);
186 error = ofproto_set_listeners(ofproto, &s.listeners);
188 ovs_fatal(error, "failed to configure management connections");
190 error = ofproto_set_snoops(ofproto, &s.snoops);
193 "failed to configure controller snooping connections");
195 memset(&nf_options, 0, sizeof nf_options);
196 nf_options.collectors = s.netflow;
197 error = ofproto_set_netflow(ofproto, &nf_options);
199 ovs_fatal(error, "failed to configure NetFlow collectors");
201 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
202 ofproto_set_probe_interval(ofproto, s.probe_interval);
203 ofproto_set_max_backoff(ofproto, s.max_backoff);
204 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
205 error = ofproto_set_stp(ofproto, s.enable_stp);
207 ovs_fatal(error, "failed to configure STP");
209 error = ofproto_set_remote_execution(ofproto, s.command_acl,
212 ovs_fatal(error, "failed to configure remote command execution");
215 error = ofproto_set_controller(ofproto, s.controller_name);
217 ovs_fatal(error, "failed to configure controller");
221 while (ofproto_is_alive(ofproto)) {
222 error = ofproto_run(ofproto);
224 ovs_fatal(error, "unrecoverable datapath error");
226 unixctl_server_run(unixctl);
230 ofproto_wait(ofproto);
231 unixctl_server_wait(unixctl);
240 /* User interface. */
243 parse_options(int argc, char *argv[], struct ofsettings *s)
246 OPT_DATAPATH_ID = UCHAR_MAX + 1,
255 OPT_INACTIVITY_PROBE,
261 OPT_BOOTSTRAP_CA_CERT,
272 LEAK_CHECKER_OPTION_ENUMS
274 static struct option long_options[] = {
275 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
276 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
277 {"hardware", required_argument, 0, OPT_HARDWARE},
278 {"software", required_argument, 0, OPT_SOFTWARE},
279 {"serial", required_argument, 0, OPT_SERIAL},
280 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
281 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
282 {"config", required_argument, 0, 'F'},
283 {"br-name", required_argument, 0, OPT_BR_NAME},
284 {"fail", required_argument, 0, OPT_FAIL_MODE},
285 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
286 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
287 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
288 {"listen", required_argument, 0, 'l'},
289 {"snoop", required_argument, 0, OPT_SNOOP},
290 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
291 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
292 {"stp", no_argument, 0, OPT_STP},
293 {"no-stp", no_argument, 0, OPT_NO_STP},
294 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
295 {"in-band", no_argument, 0, OPT_IN_BAND},
296 {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
297 {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
298 {"netflow", required_argument, 0, OPT_NETFLOW},
299 {"mgmt-id", required_argument, 0, OPT_MGMT_ID},
300 {"ports", required_argument, 0, OPT_PORTS},
301 {"verbose", optional_argument, 0, 'v'},
302 {"help", no_argument, 0, 'h'},
303 {"version", no_argument, 0, 'V'},
306 LEAK_CHECKER_LONG_OPTIONS,
308 VCONN_SSL_LONG_OPTIONS
309 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
313 char *short_options = long_options_to_short_options(long_options);
315 /* Set defaults that we can figure out before parsing options. */
320 s->serial_desc = NULL;
321 svec_init(&s->listeners);
322 svec_init(&s->snoops);
323 s->fail_mode = FAIL_OPEN;
325 s->probe_interval = 0;
327 s->update_resolv_conf = true;
330 s->accept_controller_re = NULL;
331 s->enable_stp = false;
334 s->command_dir = NULL;
335 svec_init(&s->netflow);
337 svec_init(&s->ports);
341 c = getopt_long(argc, argv, short_options, long_options, NULL);
347 case OPT_DATAPATH_ID:
348 if (!dpid_from_string(optarg, &s->datapath_id)) {
349 ovs_fatal(0, "argument to --datapath-id must be "
350 "exactly 12 hex digits and may not be all-zero");
354 case OPT_MANUFACTURER:
355 s->mfr_desc = optarg;
367 s->serial_desc = optarg;
370 case OPT_ACCEPT_VCONN:
371 s->accept_controller_re = optarg;
374 case OPT_NO_RESOLV_CONF:
375 s->update_resolv_conf = false;
379 if (!strcmp(optarg, "open")) {
380 s->fail_mode = FAIL_OPEN;
381 } else if (!strcmp(optarg, "closed")) {
382 s->fail_mode = FAIL_CLOSED;
384 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
388 case OPT_INACTIVITY_PROBE:
389 s->probe_interval = atoi(optarg);
390 if (s->probe_interval < 5) {
391 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
396 if (!strcmp(optarg, "permanent")) {
397 s->max_idle = OFP_FLOW_PERMANENT;
399 s->max_idle = atoi(optarg);
400 if (s->max_idle < 1 || s->max_idle > 65535) {
401 ovs_fatal(0, "--max-idle argument must be between 1 and "
402 "65535 or the word 'permanent'");
407 case OPT_MAX_BACKOFF:
408 s->max_backoff = atoi(optarg);
409 if (s->max_backoff < 1) {
410 ovs_fatal(0, "--max-backoff argument must be at least 1");
411 } else if (s->max_backoff > 3600) {
412 s->max_backoff = 3600;
418 s->rate_limit = atoi(optarg);
419 if (s->rate_limit < 1) {
420 ovs_fatal(0, "--rate-limit argument must be at least 1");
423 s->rate_limit = 1000;
427 case OPT_BURST_LIMIT:
428 s->burst_limit = atoi(optarg);
429 if (s->burst_limit < 1) {
430 ovs_fatal(0, "--burst-limit argument must be at least 1");
435 s->enable_stp = true;
439 s->enable_stp = false;
442 case OPT_OUT_OF_BAND:
450 case OPT_COMMAND_ACL:
451 s->command_acl = (s->command_acl[0]
452 ? xasprintf("%s,%s", s->command_acl, optarg)
456 case OPT_COMMAND_DIR:
457 s->command_dir = optarg;
461 svec_add(&s->netflow, optarg);
465 if (strlen(optarg) != 12
466 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
467 ovs_fatal(0, "argument to --mgmt-id must be "
468 "exactly 12 hex digits");
470 s->mgmt_id = strtoll(optarg, NULL, 16);
472 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
477 svec_add(&s->listeners, optarg);
481 svec_add(&s->snoops, optarg);
485 svec_split(&s->ports, optarg, ",");
492 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
495 DAEMON_OPTION_HANDLERS
499 LEAK_CHECKER_OPTION_HANDLERS
502 VCONN_SSL_OPTION_HANDLERS
504 case OPT_BOOTSTRAP_CA_CERT:
505 vconn_ssl_set_ca_cert_file(optarg, true);
520 if (argc < 1 || argc > 2) {
521 ovs_fatal(0, "need one or two non-option arguments; "
522 "use --help for usage");
525 /* Local and remote vconns. */
526 s->dp_name = argv[0];
527 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
529 /* Set accept_controller_regex. */
530 if (!s->accept_controller_re) {
531 s->accept_controller_re
532 = vconn_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
535 /* Mode of operation. */
536 s->discovery = s->controller_name == NULL;
537 if (s->discovery && !s->in_band) {
538 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
542 if (s->rate_limit && s->rate_limit < 100) {
543 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
550 printf("%s: an OpenFlow switch implementation.\n"
551 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
552 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
553 "CONTROLLER is an active OpenFlow connection method; if it is\n"
554 "omitted, then ovs-openflowd performs controller discovery.\n",
555 program_name, program_name);
556 vconn_usage(true, true, true);
557 printf("\nOpenFlow options:\n"
558 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
559 " (ID must consist of 12 hex digits)\n"
560 " --mgmt-id=ID Use ID as the management ID\n"
561 " (ID must consist of 12 hex digits)\n"
562 " --manufacturer=MFR Identify manufacturer as MFR\n"
563 " --hardware=HW Identify hardware as HW\n"
564 " --software=SW Identify software as SW\n"
565 " --serial=SERIAL Identify serial number as SERIAL\n"
566 "\nController discovery options:\n"
567 " --accept-vconn=REGEX accept matching discovered controllers\n"
568 " --no-resolv-conf do not update /etc/resolv.conf\n"
569 "\nNetworking options:\n"
570 " --fail=open|closed when controller connection fails:\n"
571 " closed: drop all packets\n"
572 " open (default): act as learning switch\n"
573 " --inactivity-probe=SECS time between inactivity probes\n"
574 " --max-idle=SECS max idle for flows set up by switch\n"
575 " --max-backoff=SECS max time between controller connection\n"
576 " attempts (default: 8 seconds)\n"
577 " -l, --listen=METHOD allow management connections on METHOD\n"
578 " (a passive OpenFlow connection method)\n"
579 " --snoop=METHOD allow controller snooping on METHOD\n"
580 " (a passive OpenFlow connection method)\n"
581 " --out-of-band controller connection is out-of-band\n"
582 " --netflow=HOST:PORT configure NetFlow output target\n"
583 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
584 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
585 " --burst-limit=BURST limit on packet credit for idle time\n"
586 "\nRemote command execution options:\n"
587 " --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
588 " --command-dir=DIR set command dir (default: %s/commands)\n",
592 printf("\nOther options:\n"
593 " -h, --help display this help message\n"
594 " -V, --version display version information\n");
595 leak_checker_usage();