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"
31 #include "discovery.h"
33 #include "fail-open.h"
36 #include "leak-checker.h"
41 #include "openflow/openflow.h"
43 #include "poll-loop.h"
50 #include "vconn-ssl.h"
54 #define THIS_MODULE VLM_secchan
56 /* Behavior when the connection to the controller fails. */
58 FAIL_OPEN, /* Act as learning switch. */
59 FAIL_CLOSED /* Drop all packets. */
62 /* Settings that may be configured by the user. */
64 /* Overall mode of operation. */
65 bool discovery; /* Discover the controller automatically? */
66 bool in_band; /* Connect to controller in-band? */
69 uint64_t datapath_id; /* Datapath ID. */
70 const char *dp_name; /* Name of local datapath. */
72 /* Description strings. */
73 const char *mfr_desc; /* Manufacturer. */
74 const char *hw_desc; /* Hardware. */
75 const char *sw_desc; /* Software version. */
76 const char *serial_desc; /* Serial number. */
78 /* Related vconns and network devices. */
79 const char *controller_name; /* Controller (if not discovery mode). */
80 struct svec listeners; /* Listen for management connections. */
81 struct svec snoops; /* Listen for controller snooping conns. */
83 /* Failure behavior. */
84 enum fail_mode fail_mode; /* Act as learning switch if no controller? */
85 int max_idle; /* Idle time for flows in fail-open mode. */
86 int probe_interval; /* # seconds idle before sending echo request. */
87 int max_backoff; /* Max # seconds between connection attempts. */
89 /* Packet-in rate-limiting. */
90 int rate_limit; /* Tokens added to bucket per second. */
91 int burst_limit; /* Maximum number token bucket size. */
93 /* Discovery behavior. */
94 const char *accept_controller_re; /* Controller vconns to accept. */
95 bool update_resolv_conf; /* Update /etc/resolv.conf? */
97 /* Spanning tree protocol. */
100 /* Remote command execution. */
101 char *command_acl; /* Command white/blacklist, as shell globs. */
102 char *command_dir; /* Directory that contains commands. */
105 uint64_t mgmt_id; /* Management ID. */
108 struct svec netflow; /* NetFlow targets. */
111 static void parse_options(int argc, char *argv[], struct ofsettings *);
112 static void usage(void) NO_RETURN;
115 main(int argc, char *argv[])
117 struct unixctl_server *unixctl;
118 struct ofproto *ofproto;
122 set_program_name(argv[0]);
123 register_fault_handlers();
126 parse_options(argc, argv, &s);
127 signal(SIGPIPE, SIG_IGN);
129 die_if_already_running();
132 /* Start listening for ovs-appctl requests. */
133 error = unixctl_server_create(NULL, &unixctl);
135 ovs_fatal(error, "Could not listen for unixctl connections");
138 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
139 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
141 /* Start OpenFlow processing. */
142 error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
144 ovs_fatal(error, "could not initialize openflow switch");
146 error = ofproto_set_in_band(ofproto, s.in_band);
148 ovs_fatal(error, "failed to configure in-band control");
150 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
151 s.update_resolv_conf);
153 ovs_fatal(error, "failed to configure controller discovery");
156 ofproto_set_datapath_id(ofproto, s.datapath_id);
159 ofproto_set_mgmt_id(ofproto, s.mgmt_id);
161 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc, s.serial_desc);
162 error = ofproto_set_listeners(ofproto, &s.listeners);
164 ovs_fatal(error, "failed to configure management connections");
166 error = ofproto_set_snoops(ofproto, &s.snoops);
169 "failed to configure controller snooping connections");
171 error = ofproto_set_netflow(ofproto, &s.netflow, 0, 0, false);
173 ovs_fatal(error, "failed to configure NetFlow collectors");
175 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
176 ofproto_set_probe_interval(ofproto, s.probe_interval);
177 ofproto_set_max_backoff(ofproto, s.max_backoff);
178 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
179 error = ofproto_set_stp(ofproto, s.enable_stp);
181 ovs_fatal(error, "failed to configure STP");
183 error = ofproto_set_remote_execution(ofproto, s.command_acl,
186 ovs_fatal(error, "failed to configure remote command execution");
189 error = ofproto_set_controller(ofproto, s.controller_name);
191 ovs_fatal(error, "failed to configure controller");
195 while (ofproto_is_alive(ofproto)) {
196 error = ofproto_run(ofproto);
198 ovs_fatal(error, "unrecoverable datapath error");
200 unixctl_server_run(unixctl);
202 ofproto_wait(ofproto);
203 unixctl_server_wait(unixctl);
210 /* User interface. */
213 parse_options(int argc, char *argv[], struct ofsettings *s)
216 OPT_DATAPATH_ID = UCHAR_MAX + 1,
225 OPT_INACTIVITY_PROBE,
231 OPT_BOOTSTRAP_CA_CERT,
241 LEAK_CHECKER_OPTION_ENUMS
243 static struct option long_options[] = {
244 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
245 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
246 {"hardware", required_argument, 0, OPT_HARDWARE},
247 {"software", required_argument, 0, OPT_SOFTWARE},
248 {"serial", required_argument, 0, OPT_SERIAL},
249 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
250 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
251 {"config", required_argument, 0, 'F'},
252 {"br-name", required_argument, 0, OPT_BR_NAME},
253 {"fail", required_argument, 0, OPT_FAIL_MODE},
254 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
255 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
256 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
257 {"listen", required_argument, 0, 'l'},
258 {"snoop", required_argument, 0, OPT_SNOOP},
259 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
260 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
261 {"stp", no_argument, 0, OPT_STP},
262 {"no-stp", no_argument, 0, OPT_NO_STP},
263 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
264 {"in-band", no_argument, 0, OPT_IN_BAND},
265 {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
266 {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
267 {"netflow", required_argument, 0, OPT_NETFLOW},
268 {"mgmt-id", required_argument, 0, OPT_MGMT_ID},
269 {"verbose", optional_argument, 0, 'v'},
270 {"help", no_argument, 0, 'h'},
271 {"version", no_argument, 0, 'V'},
274 LEAK_CHECKER_LONG_OPTIONS,
276 VCONN_SSL_LONG_OPTIONS
277 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
281 char *short_options = long_options_to_short_options(long_options);
283 /* Set defaults that we can figure out before parsing options. */
288 s->serial_desc = NULL;
289 svec_init(&s->listeners);
290 svec_init(&s->snoops);
291 s->fail_mode = FAIL_OPEN;
293 s->probe_interval = 0;
295 s->update_resolv_conf = true;
298 s->accept_controller_re = NULL;
299 s->enable_stp = false;
302 s->command_dir = NULL;
303 svec_init(&s->netflow);
308 c = getopt_long(argc, argv, short_options, long_options, NULL);
314 case OPT_DATAPATH_ID:
315 if (strlen(optarg) != 12
316 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
317 ovs_fatal(0, "argument to --datapath-id must be "
318 "exactly 12 hex digits");
320 s->datapath_id = strtoll(optarg, NULL, 16);
321 if (!s->datapath_id) {
322 ovs_fatal(0, "argument to --datapath-id must be nonzero");
326 case OPT_MANUFACTURER:
327 s->mfr_desc = optarg;
339 s->serial_desc = optarg;
342 case OPT_ACCEPT_VCONN:
343 s->accept_controller_re = optarg;
346 case OPT_NO_RESOLV_CONF:
347 s->update_resolv_conf = false;
351 if (!strcmp(optarg, "open")) {
352 s->fail_mode = FAIL_OPEN;
353 } else if (!strcmp(optarg, "closed")) {
354 s->fail_mode = FAIL_CLOSED;
356 ovs_fatal(0, "-f or --fail argument must be \"open\" "
361 case OPT_INACTIVITY_PROBE:
362 s->probe_interval = atoi(optarg);
363 if (s->probe_interval < 5) {
364 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
369 if (!strcmp(optarg, "permanent")) {
370 s->max_idle = OFP_FLOW_PERMANENT;
372 s->max_idle = atoi(optarg);
373 if (s->max_idle < 1 || s->max_idle > 65535) {
374 ovs_fatal(0, "--max-idle argument must be between 1 and "
375 "65535 or the word 'permanent'");
380 case OPT_MAX_BACKOFF:
381 s->max_backoff = atoi(optarg);
382 if (s->max_backoff < 1) {
383 ovs_fatal(0, "--max-backoff argument must be at least 1");
384 } else if (s->max_backoff > 3600) {
385 s->max_backoff = 3600;
391 s->rate_limit = atoi(optarg);
392 if (s->rate_limit < 1) {
393 ovs_fatal(0, "--rate-limit argument must be at least 1");
396 s->rate_limit = 1000;
400 case OPT_BURST_LIMIT:
401 s->burst_limit = atoi(optarg);
402 if (s->burst_limit < 1) {
403 ovs_fatal(0, "--burst-limit argument must be at least 1");
408 s->enable_stp = true;
412 s->enable_stp = false;
415 case OPT_OUT_OF_BAND:
423 case OPT_COMMAND_ACL:
424 s->command_acl = (s->command_acl[0]
425 ? xasprintf("%s,%s", s->command_acl, optarg)
429 case OPT_COMMAND_DIR:
430 s->command_dir = optarg;
434 svec_add(&s->netflow, optarg);
438 if (strlen(optarg) != 12
439 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
440 ovs_fatal(0, "argument to --mgmt-id must be "
441 "exactly 12 hex digits");
443 s->mgmt_id = strtoll(optarg, NULL, 16);
445 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
450 svec_add(&s->listeners, optarg);
454 svec_add(&s->snoops, optarg);
461 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
464 DAEMON_OPTION_HANDLERS
468 LEAK_CHECKER_OPTION_HANDLERS
471 VCONN_SSL_OPTION_HANDLERS
473 case OPT_BOOTSTRAP_CA_CERT:
474 vconn_ssl_set_ca_cert_file(optarg, true);
489 if (argc < 1 || argc > 2) {
490 ovs_fatal(0, "need one or two non-option arguments; "
491 "use --help for usage");
494 /* Local and remote vconns. */
495 s->dp_name = argv[0];
496 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
498 /* Set accept_controller_regex. */
499 if (!s->accept_controller_re) {
500 s->accept_controller_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
503 /* Mode of operation. */
504 s->discovery = s->controller_name == NULL;
505 if (s->discovery && !s->in_band) {
506 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
510 if (s->rate_limit && s->rate_limit < 100) {
511 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
518 printf("%s: an OpenFlow switch implementation.\n"
519 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
520 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
521 "CONTROLLER is an active OpenFlow connection method; if it is\n"
522 "omitted, then secchan performs controller discovery.\n",
523 program_name, program_name);
524 vconn_usage(true, true, true);
525 printf("\nOpenFlow options:\n"
526 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
527 " (ID must consist of 12 hex digits)\n"
528 " --mgmt-id=ID Use ID as the management ID\n"
529 " (ID must consist of 12 hex digits)\n"
530 " --manufacturer=MFR Identify manufacturer as MFR\n"
531 " --hardware=HW Identify hardware as HW\n"
532 " --software=SW Identify software as SW\n"
533 " --serial=SERIAL Identify serial number as SERIAL\n"
534 "\nController discovery options:\n"
535 " --accept-vconn=REGEX accept matching discovered controllers\n"
536 " --no-resolv-conf do not update /etc/resolv.conf\n"
537 "\nNetworking options:\n"
538 " --fail=open|closed when controller connection fails:\n"
539 " closed: drop all packets\n"
540 " open (default): act as learning switch\n"
541 " --inactivity-probe=SECS time between inactivity probes\n"
542 " --max-idle=SECS max idle for flows set up by secchan\n"
543 " --max-backoff=SECS max time between controller connection\n"
544 " attempts (default: 15 seconds)\n"
545 " -l, --listen=METHOD allow management connections on METHOD\n"
546 " (a passive OpenFlow connection method)\n"
547 " --snoop=METHOD allow controller snooping on METHOD\n"
548 " (a passive OpenFlow connection method)\n"
549 " --out-of-band controller connection is out-of-band\n"
550 " --netflow=HOST:PORT configure NetFlow output target\n"
551 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
552 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
553 " --burst-limit=BURST limit on packet credit for idle time\n"
554 "\nRemote command execution options:\n"
555 " --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
556 " --command-dir=DIR set command dir (default: %s/commands)\n",
560 printf("\nOther options:\n"
561 " -h, --help display this help message\n"
562 " -V, --version display version information\n");
563 leak_checker_usage();