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. */
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. */
96 /* Remote command execution. */
97 char *command_acl; /* Command white/blacklist, as shell globs. */
98 char *command_dir; /* Directory that contains commands. */
101 uint64_t mgmt_id; /* Management ID. */
104 struct svec netflow; /* NetFlow targets. */
107 static void parse_options(int argc, char *argv[], struct ofsettings *);
108 static void usage(void) NO_RETURN;
111 main(int argc, char *argv[])
113 struct unixctl_server *unixctl;
114 struct ofproto *ofproto;
118 set_program_name(argv[0]);
119 register_fault_handlers();
122 parse_options(argc, argv, &s);
123 signal(SIGPIPE, SIG_IGN);
125 die_if_already_running();
128 /* Start listening for ovs-appctl requests. */
129 error = unixctl_server_create(NULL, &unixctl);
131 ovs_fatal(error, "Could not listen for unixctl connections");
134 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
135 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
137 /* Start OpenFlow processing. */
138 error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
140 ovs_fatal(error, "could not initialize openflow switch");
142 error = ofproto_set_in_band(ofproto, s.in_band);
144 ovs_fatal(error, "failed to configure in-band control");
146 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
147 s.update_resolv_conf);
149 ovs_fatal(error, "failed to configure controller discovery");
152 ofproto_set_datapath_id(ofproto, s.datapath_id);
155 ofproto_set_mgmt_id(ofproto, s.mgmt_id);
157 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc, s.serial_desc);
158 error = ofproto_set_listeners(ofproto, &s.listeners);
160 ovs_fatal(error, "failed to configure management connections");
162 error = ofproto_set_snoops(ofproto, &s.snoops);
165 "failed to configure controller snooping connections");
167 error = ofproto_set_netflow(ofproto, &s.netflow, 0, 0, false);
169 ovs_fatal(error, "failed to configure NetFlow collectors");
171 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
172 ofproto_set_probe_interval(ofproto, s.probe_interval);
173 ofproto_set_max_backoff(ofproto, s.max_backoff);
174 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
175 error = ofproto_set_stp(ofproto, s.enable_stp);
177 ovs_fatal(error, "failed to configure STP");
179 error = ofproto_set_remote_execution(ofproto, s.command_acl,
182 ovs_fatal(error, "failed to configure remote command execution");
185 error = ofproto_set_controller(ofproto, s.controller_name);
187 ovs_fatal(error, "failed to configure controller");
191 while (ofproto_is_alive(ofproto)) {
192 error = ofproto_run(ofproto);
194 ovs_fatal(error, "unrecoverable datapath error");
196 unixctl_server_run(unixctl);
199 ofproto_wait(ofproto);
200 unixctl_server_wait(unixctl);
208 /* User interface. */
211 parse_options(int argc, char *argv[], struct ofsettings *s)
214 OPT_DATAPATH_ID = UCHAR_MAX + 1,
223 OPT_INACTIVITY_PROBE,
229 OPT_BOOTSTRAP_CA_CERT,
239 LEAK_CHECKER_OPTION_ENUMS
241 static struct option long_options[] = {
242 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
243 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
244 {"hardware", required_argument, 0, OPT_HARDWARE},
245 {"software", required_argument, 0, OPT_SOFTWARE},
246 {"serial", required_argument, 0, OPT_SERIAL},
247 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
248 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
249 {"config", required_argument, 0, 'F'},
250 {"br-name", required_argument, 0, OPT_BR_NAME},
251 {"fail", required_argument, 0, OPT_FAIL_MODE},
252 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
253 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
254 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
255 {"listen", required_argument, 0, 'l'},
256 {"snoop", required_argument, 0, OPT_SNOOP},
257 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
258 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
259 {"stp", no_argument, 0, OPT_STP},
260 {"no-stp", no_argument, 0, OPT_NO_STP},
261 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
262 {"in-band", no_argument, 0, OPT_IN_BAND},
263 {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
264 {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
265 {"netflow", required_argument, 0, OPT_NETFLOW},
266 {"mgmt-id", required_argument, 0, OPT_MGMT_ID},
267 {"verbose", optional_argument, 0, 'v'},
268 {"help", no_argument, 0, 'h'},
269 {"version", no_argument, 0, 'V'},
272 LEAK_CHECKER_LONG_OPTIONS,
274 VCONN_SSL_LONG_OPTIONS
275 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
279 char *short_options = long_options_to_short_options(long_options);
281 /* Set defaults that we can figure out before parsing options. */
286 s->serial_desc = NULL;
287 svec_init(&s->listeners);
288 svec_init(&s->snoops);
289 s->fail_mode = FAIL_OPEN;
291 s->probe_interval = 0;
293 s->update_resolv_conf = true;
296 s->accept_controller_re = NULL;
297 s->enable_stp = false;
300 s->command_dir = NULL;
301 svec_init(&s->netflow);
306 c = getopt_long(argc, argv, short_options, long_options, NULL);
312 case OPT_DATAPATH_ID:
313 if (strlen(optarg) != 12
314 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
315 ovs_fatal(0, "argument to --datapath-id must be "
316 "exactly 12 hex digits");
318 s->datapath_id = strtoll(optarg, NULL, 16);
319 if (!s->datapath_id) {
320 ovs_fatal(0, "argument to --datapath-id must be nonzero");
324 case OPT_MANUFACTURER:
325 s->mfr_desc = optarg;
337 s->serial_desc = optarg;
340 case OPT_ACCEPT_VCONN:
341 s->accept_controller_re = optarg;
344 case OPT_NO_RESOLV_CONF:
345 s->update_resolv_conf = false;
349 if (!strcmp(optarg, "open")) {
350 s->fail_mode = FAIL_OPEN;
351 } else if (!strcmp(optarg, "closed")) {
352 s->fail_mode = FAIL_CLOSED;
354 ovs_fatal(0, "-f or --fail argument must be \"open\" "
359 case OPT_INACTIVITY_PROBE:
360 s->probe_interval = atoi(optarg);
361 if (s->probe_interval < 5) {
362 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
367 if (!strcmp(optarg, "permanent")) {
368 s->max_idle = OFP_FLOW_PERMANENT;
370 s->max_idle = atoi(optarg);
371 if (s->max_idle < 1 || s->max_idle > 65535) {
372 ovs_fatal(0, "--max-idle argument must be between 1 and "
373 "65535 or the word 'permanent'");
378 case OPT_MAX_BACKOFF:
379 s->max_backoff = atoi(optarg);
380 if (s->max_backoff < 1) {
381 ovs_fatal(0, "--max-backoff argument must be at least 1");
382 } else if (s->max_backoff > 3600) {
383 s->max_backoff = 3600;
389 s->rate_limit = atoi(optarg);
390 if (s->rate_limit < 1) {
391 ovs_fatal(0, "--rate-limit argument must be at least 1");
394 s->rate_limit = 1000;
398 case OPT_BURST_LIMIT:
399 s->burst_limit = atoi(optarg);
400 if (s->burst_limit < 1) {
401 ovs_fatal(0, "--burst-limit argument must be at least 1");
406 s->enable_stp = true;
410 s->enable_stp = false;
413 case OPT_OUT_OF_BAND:
421 case OPT_COMMAND_ACL:
422 s->command_acl = (s->command_acl[0]
423 ? xasprintf("%s,%s", s->command_acl, optarg)
427 case OPT_COMMAND_DIR:
428 s->command_dir = optarg;
432 svec_add(&s->netflow, optarg);
436 if (strlen(optarg) != 12
437 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
438 ovs_fatal(0, "argument to --mgmt-id must be "
439 "exactly 12 hex digits");
441 s->mgmt_id = strtoll(optarg, NULL, 16);
443 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
448 svec_add(&s->listeners, optarg);
452 svec_add(&s->snoops, optarg);
459 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
462 DAEMON_OPTION_HANDLERS
466 LEAK_CHECKER_OPTION_HANDLERS
469 VCONN_SSL_OPTION_HANDLERS
471 case OPT_BOOTSTRAP_CA_CERT:
472 vconn_ssl_set_ca_cert_file(optarg, true);
487 if (argc < 1 || argc > 2) {
488 ovs_fatal(0, "need one or two non-option arguments; "
489 "use --help for usage");
492 /* Local and remote vconns. */
493 s->dp_name = argv[0];
494 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
496 /* Set accept_controller_regex. */
497 if (!s->accept_controller_re) {
498 s->accept_controller_re
499 = vconn_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
502 /* Mode of operation. */
503 s->discovery = s->controller_name == NULL;
504 if (s->discovery && !s->in_band) {
505 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
509 if (s->rate_limit && s->rate_limit < 100) {
510 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
517 printf("%s: an OpenFlow switch implementation.\n"
518 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
519 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
520 "CONTROLLER is an active OpenFlow connection method; if it is\n"
521 "omitted, then ovs-openflowd performs controller discovery.\n",
522 program_name, program_name);
523 vconn_usage(true, true, true);
524 printf("\nOpenFlow options:\n"
525 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
526 " (ID must consist of 12 hex digits)\n"
527 " --mgmt-id=ID Use ID as the management ID\n"
528 " (ID must consist of 12 hex digits)\n"
529 " --manufacturer=MFR Identify manufacturer as MFR\n"
530 " --hardware=HW Identify hardware as HW\n"
531 " --software=SW Identify software as SW\n"
532 " --serial=SERIAL Identify serial number as SERIAL\n"
533 "\nController discovery options:\n"
534 " --accept-vconn=REGEX accept matching discovered controllers\n"
535 " --no-resolv-conf do not update /etc/resolv.conf\n"
536 "\nNetworking options:\n"
537 " --fail=open|closed when controller connection fails:\n"
538 " closed: drop all packets\n"
539 " open (default): act as learning switch\n"
540 " --inactivity-probe=SECS time between inactivity probes\n"
541 " --max-idle=SECS max idle for flows set up by switch\n"
542 " --max-backoff=SECS max time between controller connection\n"
543 " attempts (default: 15 seconds)\n"
544 " -l, --listen=METHOD allow management connections on METHOD\n"
545 " (a passive OpenFlow connection method)\n"
546 " --snoop=METHOD allow controller snooping on METHOD\n"
547 " (a passive OpenFlow connection method)\n"
548 " --out-of-band controller connection is out-of-band\n"
549 " --netflow=HOST:PORT configure NetFlow output target\n"
550 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
551 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
552 " --burst-limit=BURST limit on packet credit for idle time\n"
553 "\nRemote command execution options:\n"
554 " --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
555 " --command-dir=DIR set command dir (default: %s/commands)\n",
559 printf("\nOther options:\n"
560 " -h, --help display this help message\n"
561 " -V, --version display version information\n");
562 leak_checker_usage();