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;
121 struct netflow_options nf_options;
123 set_program_name(argv[0]);
124 register_fault_handlers();
127 parse_options(argc, argv, &s);
128 signal(SIGPIPE, SIG_IGN);
130 die_if_already_running();
133 /* Start listening for ovs-appctl requests. */
134 error = unixctl_server_create(NULL, &unixctl);
136 ovs_fatal(error, "Could not listen for unixctl connections");
139 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
140 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
142 /* Start OpenFlow processing. */
143 error = ofproto_create(s.dp_name, NULL, NULL, &ofproto);
145 ovs_fatal(error, "could not initialize openflow switch");
147 error = ofproto_set_in_band(ofproto, s.in_band);
149 ovs_fatal(error, "failed to configure in-band control");
151 error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
152 s.update_resolv_conf);
154 ovs_fatal(error, "failed to configure controller discovery");
157 ofproto_set_datapath_id(ofproto, s.datapath_id);
160 ofproto_set_mgmt_id(ofproto, s.mgmt_id);
162 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc, s.serial_desc);
163 error = ofproto_set_listeners(ofproto, &s.listeners);
165 ovs_fatal(error, "failed to configure management connections");
167 error = ofproto_set_snoops(ofproto, &s.snoops);
170 "failed to configure controller snooping connections");
172 memset(&nf_options, 0, sizeof nf_options);
173 nf_options.collectors = s.netflow;
174 error = ofproto_set_netflow(ofproto, &nf_options);
176 ovs_fatal(error, "failed to configure NetFlow collectors");
178 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
179 ofproto_set_probe_interval(ofproto, s.probe_interval);
180 ofproto_set_max_backoff(ofproto, s.max_backoff);
181 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
182 error = ofproto_set_stp(ofproto, s.enable_stp);
184 ovs_fatal(error, "failed to configure STP");
186 error = ofproto_set_remote_execution(ofproto, s.command_acl,
189 ovs_fatal(error, "failed to configure remote command execution");
192 error = ofproto_set_controller(ofproto, s.controller_name);
194 ovs_fatal(error, "failed to configure controller");
198 while (ofproto_is_alive(ofproto)) {
199 error = ofproto_run(ofproto);
201 ovs_fatal(error, "unrecoverable datapath error");
203 unixctl_server_run(unixctl);
205 ofproto_wait(ofproto);
206 unixctl_server_wait(unixctl);
213 /* User interface. */
216 parse_options(int argc, char *argv[], struct ofsettings *s)
219 OPT_DATAPATH_ID = UCHAR_MAX + 1,
228 OPT_INACTIVITY_PROBE,
234 OPT_BOOTSTRAP_CA_CERT,
244 LEAK_CHECKER_OPTION_ENUMS
246 static struct option long_options[] = {
247 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
248 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
249 {"hardware", required_argument, 0, OPT_HARDWARE},
250 {"software", required_argument, 0, OPT_SOFTWARE},
251 {"serial", required_argument, 0, OPT_SERIAL},
252 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
253 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
254 {"config", required_argument, 0, 'F'},
255 {"br-name", required_argument, 0, OPT_BR_NAME},
256 {"fail", required_argument, 0, OPT_FAIL_MODE},
257 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
258 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
259 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
260 {"listen", required_argument, 0, 'l'},
261 {"snoop", required_argument, 0, OPT_SNOOP},
262 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
263 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
264 {"stp", no_argument, 0, OPT_STP},
265 {"no-stp", no_argument, 0, OPT_NO_STP},
266 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
267 {"in-band", no_argument, 0, OPT_IN_BAND},
268 {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
269 {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
270 {"netflow", required_argument, 0, OPT_NETFLOW},
271 {"mgmt-id", required_argument, 0, OPT_MGMT_ID},
272 {"verbose", optional_argument, 0, 'v'},
273 {"help", no_argument, 0, 'h'},
274 {"version", no_argument, 0, 'V'},
277 LEAK_CHECKER_LONG_OPTIONS,
279 VCONN_SSL_LONG_OPTIONS
280 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
284 char *short_options = long_options_to_short_options(long_options);
286 /* Set defaults that we can figure out before parsing options. */
291 s->serial_desc = NULL;
292 svec_init(&s->listeners);
293 svec_init(&s->snoops);
294 s->fail_mode = FAIL_OPEN;
296 s->probe_interval = 0;
298 s->update_resolv_conf = true;
301 s->accept_controller_re = NULL;
302 s->enable_stp = false;
305 s->command_dir = NULL;
306 svec_init(&s->netflow);
311 c = getopt_long(argc, argv, short_options, long_options, NULL);
317 case OPT_DATAPATH_ID:
318 if (strlen(optarg) != 12
319 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
320 ovs_fatal(0, "argument to --datapath-id must be "
321 "exactly 12 hex digits");
323 s->datapath_id = strtoll(optarg, NULL, 16);
324 if (!s->datapath_id) {
325 ovs_fatal(0, "argument to --datapath-id must be nonzero");
329 case OPT_MANUFACTURER:
330 s->mfr_desc = optarg;
342 s->serial_desc = optarg;
345 case OPT_ACCEPT_VCONN:
346 s->accept_controller_re = optarg;
349 case OPT_NO_RESOLV_CONF:
350 s->update_resolv_conf = false;
354 if (!strcmp(optarg, "open")) {
355 s->fail_mode = FAIL_OPEN;
356 } else if (!strcmp(optarg, "closed")) {
357 s->fail_mode = FAIL_CLOSED;
359 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
363 case OPT_INACTIVITY_PROBE:
364 s->probe_interval = atoi(optarg);
365 if (s->probe_interval < 5) {
366 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
371 if (!strcmp(optarg, "permanent")) {
372 s->max_idle = OFP_FLOW_PERMANENT;
374 s->max_idle = atoi(optarg);
375 if (s->max_idle < 1 || s->max_idle > 65535) {
376 ovs_fatal(0, "--max-idle argument must be between 1 and "
377 "65535 or the word 'permanent'");
382 case OPT_MAX_BACKOFF:
383 s->max_backoff = atoi(optarg);
384 if (s->max_backoff < 1) {
385 ovs_fatal(0, "--max-backoff argument must be at least 1");
386 } else if (s->max_backoff > 3600) {
387 s->max_backoff = 3600;
393 s->rate_limit = atoi(optarg);
394 if (s->rate_limit < 1) {
395 ovs_fatal(0, "--rate-limit argument must be at least 1");
398 s->rate_limit = 1000;
402 case OPT_BURST_LIMIT:
403 s->burst_limit = atoi(optarg);
404 if (s->burst_limit < 1) {
405 ovs_fatal(0, "--burst-limit argument must be at least 1");
410 s->enable_stp = true;
414 s->enable_stp = false;
417 case OPT_OUT_OF_BAND:
425 case OPT_COMMAND_ACL:
426 s->command_acl = (s->command_acl[0]
427 ? xasprintf("%s,%s", s->command_acl, optarg)
431 case OPT_COMMAND_DIR:
432 s->command_dir = optarg;
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);
463 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
466 DAEMON_OPTION_HANDLERS
470 LEAK_CHECKER_OPTION_HANDLERS
473 VCONN_SSL_OPTION_HANDLERS
475 case OPT_BOOTSTRAP_CA_CERT:
476 vconn_ssl_set_ca_cert_file(optarg, true);
491 if (argc < 1 || argc > 2) {
492 ovs_fatal(0, "need one or two non-option arguments; "
493 "use --help for usage");
496 /* Local and remote vconns. */
497 s->dp_name = argv[0];
498 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
500 /* Set accept_controller_regex. */
501 if (!s->accept_controller_re) {
502 s->accept_controller_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
505 /* Mode of operation. */
506 s->discovery = s->controller_name == NULL;
507 if (s->discovery && !s->in_band) {
508 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
512 if (s->rate_limit && s->rate_limit < 100) {
513 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
520 printf("%s: an OpenFlow switch implementation.\n"
521 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
522 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
523 "CONTROLLER is an active OpenFlow connection method; if it is\n"
524 "omitted, then secchan performs controller discovery.\n",
525 program_name, program_name);
526 vconn_usage(true, true, true);
527 printf("\nOpenFlow options:\n"
528 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
529 " (ID must consist of 12 hex digits)\n"
530 " --mgmt-id=ID Use ID as the management ID\n"
531 " (ID must consist of 12 hex digits)\n"
532 " --manufacturer=MFR Identify manufacturer as MFR\n"
533 " --hardware=HW Identify hardware as HW\n"
534 " --software=SW Identify software as SW\n"
535 " --serial=SERIAL Identify serial number as SERIAL\n"
536 "\nController discovery options:\n"
537 " --accept-vconn=REGEX accept matching discovered controllers\n"
538 " --no-resolv-conf do not update /etc/resolv.conf\n"
539 "\nNetworking options:\n"
540 " --fail=open|closed when controller connection fails:\n"
541 " closed: drop all packets\n"
542 " open (default): act as learning switch\n"
543 " --inactivity-probe=SECS time between inactivity probes\n"
544 " --max-idle=SECS max idle for flows set up by secchan\n"
545 " --max-backoff=SECS max time between controller connection\n"
546 " attempts (default: 8 seconds)\n"
547 " -l, --listen=METHOD allow management connections on METHOD\n"
548 " (a passive OpenFlow connection method)\n"
549 " --snoop=METHOD allow controller snooping on METHOD\n"
550 " (a passive OpenFlow connection method)\n"
551 " --out-of-band controller connection is out-of-band\n"
552 " --netflow=HOST:PORT configure NetFlow output target\n"
553 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
554 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
555 " --burst-limit=BURST limit on packet credit for idle time\n"
556 "\nRemote command execution options:\n"
557 " --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
558 " --command-dir=DIR set command dir (default: %s/commands)\n",
562 printf("\nOther options:\n"
563 " -h, --help display this help message\n"
564 " -V, --version display version information\n");
565 leak_checker_usage();