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"
32 #include "leak-checker.h"
36 #include "ofproto/ofproto.h"
37 #include "openflow/openflow.h"
39 #include "poll-loop.h"
41 #include "stream-ssl.h"
49 #define THIS_MODULE VLM_openflowd
51 /* Behavior when the connection to the controller fails. */
53 FAIL_OPEN, /* Act as learning switch. */
54 FAIL_CLOSED /* Drop all packets. */
57 /* Settings that may be configured by the user. */
59 /* Overall mode of operation. */
60 bool discovery; /* Discover the controller automatically? */
61 bool in_band; /* Connect to controller in-band? */
64 uint64_t datapath_id; /* Datapath ID. */
65 char *dp_name; /* Name of local datapath. */
66 char *dp_type; /* Type 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. */
98 uint64_t mgmt_id; /* Management ID. */
101 struct svec netflow; /* NetFlow targets. */
104 static void parse_options(int argc, char *argv[], struct ofsettings *);
105 static void usage(void) NO_RETURN;
108 main(int argc, char *argv[])
110 struct unixctl_server *unixctl;
111 struct ofproto *ofproto;
115 struct netflow_options nf_options;
117 proctitle_init(argc, argv);
118 set_program_name(argv[0]);
121 parse_options(argc, argv, &s);
122 signal(SIGPIPE, SIG_IGN);
124 die_if_already_running();
127 /* Start listening for ovs-appctl requests. */
128 error = unixctl_server_create(NULL, &unixctl);
133 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
134 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
136 error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
138 ovs_fatal(error, "could not create datapath");
141 /* Add ports to the datapath if requested by the user. */
145 struct netdev *netdev;
147 SVEC_FOR_EACH (i, port, &s.ports) {
148 error = netdev_open_default(port, &netdev);
150 ovs_fatal(error, "failed to open %s as a device", port);
153 error = dpif_port_add(dpif, port, 0, NULL);
155 ovs_fatal(error, "failed to add %s as a port", port);
160 /* Start OpenFlow processing. */
161 error = ofproto_create(s.dp_name, s.dp_type, 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, s.serial_desc);
181 if (!s.listeners.n) {
182 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
183 ovs_rundir, s.dp_name));
184 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
185 svec_clear(&s.listeners);
187 error = ofproto_set_listeners(ofproto, &s.listeners);
189 ovs_fatal(error, "failed to configure management connections");
191 error = ofproto_set_snoops(ofproto, &s.snoops);
194 "failed to configure controller snooping connections");
196 memset(&nf_options, 0, sizeof nf_options);
197 nf_options.collectors = s.netflow;
198 error = ofproto_set_netflow(ofproto, &nf_options);
200 ovs_fatal(error, "failed to configure NetFlow collectors");
202 ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
203 ofproto_set_probe_interval(ofproto, s.probe_interval);
204 ofproto_set_max_backoff(ofproto, s.max_backoff);
205 ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
206 error = ofproto_set_stp(ofproto, s.enable_stp);
208 ovs_fatal(error, "failed to configure STP");
211 error = ofproto_set_controller(ofproto, s.controller_name);
213 ovs_fatal(error, "failed to configure controller");
217 daemonize_complete();
219 while (ofproto_is_alive(ofproto)) {
220 error = ofproto_run(ofproto);
222 ovs_fatal(error, "unrecoverable datapath error");
224 unixctl_server_run(unixctl);
228 ofproto_wait(ofproto);
229 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,
270 LEAK_CHECKER_OPTION_ENUMS
272 static struct option long_options[] = {
273 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
274 {"manufacturer", required_argument, 0, OPT_MANUFACTURER},
275 {"hardware", required_argument, 0, OPT_HARDWARE},
276 {"software", required_argument, 0, OPT_SOFTWARE},
277 {"serial", required_argument, 0, OPT_SERIAL},
278 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
279 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
280 {"config", required_argument, 0, 'F'},
281 {"br-name", required_argument, 0, OPT_BR_NAME},
282 {"fail", required_argument, 0, OPT_FAIL_MODE},
283 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
284 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
285 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
286 {"listen", required_argument, 0, 'l'},
287 {"snoop", required_argument, 0, OPT_SNOOP},
288 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
289 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
290 {"stp", no_argument, 0, OPT_STP},
291 {"no-stp", no_argument, 0, OPT_NO_STP},
292 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
293 {"in-band", no_argument, 0, OPT_IN_BAND},
294 {"netflow", required_argument, 0, OPT_NETFLOW},
295 {"mgmt-id", required_argument, 0, OPT_MGMT_ID},
296 {"ports", required_argument, 0, OPT_PORTS},
297 {"verbose", optional_argument, 0, 'v'},
298 {"help", no_argument, 0, 'h'},
299 {"version", no_argument, 0, 'V'},
302 LEAK_CHECKER_LONG_OPTIONS,
304 STREAM_SSL_LONG_OPTIONS
305 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
309 char *short_options = long_options_to_short_options(long_options);
311 /* Set defaults that we can figure out before parsing options. */
316 s->serial_desc = NULL;
317 svec_init(&s->listeners);
318 svec_init(&s->snoops);
319 s->fail_mode = FAIL_OPEN;
321 s->probe_interval = 0;
323 s->update_resolv_conf = true;
326 s->accept_controller_re = NULL;
327 s->enable_stp = false;
329 svec_init(&s->netflow);
331 svec_init(&s->ports);
335 c = getopt_long(argc, argv, short_options, long_options, NULL);
341 case OPT_DATAPATH_ID:
342 if (!dpid_from_string(optarg, &s->datapath_id)) {
343 ovs_fatal(0, "argument to --datapath-id must be "
344 "exactly 12 hex digits and may not be all-zero");
348 case OPT_MANUFACTURER:
349 s->mfr_desc = optarg;
361 s->serial_desc = optarg;
364 case OPT_ACCEPT_VCONN:
365 s->accept_controller_re = optarg;
368 case OPT_NO_RESOLV_CONF:
369 s->update_resolv_conf = false;
373 if (!strcmp(optarg, "open")) {
374 s->fail_mode = FAIL_OPEN;
375 } else if (!strcmp(optarg, "closed")) {
376 s->fail_mode = FAIL_CLOSED;
378 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
382 case OPT_INACTIVITY_PROBE:
383 s->probe_interval = atoi(optarg);
384 if (s->probe_interval < 5) {
385 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
390 if (!strcmp(optarg, "permanent")) {
391 s->max_idle = OFP_FLOW_PERMANENT;
393 s->max_idle = atoi(optarg);
394 if (s->max_idle < 1 || s->max_idle > 65535) {
395 ovs_fatal(0, "--max-idle argument must be between 1 and "
396 "65535 or the word 'permanent'");
401 case OPT_MAX_BACKOFF:
402 s->max_backoff = atoi(optarg);
403 if (s->max_backoff < 1) {
404 ovs_fatal(0, "--max-backoff argument must be at least 1");
405 } else if (s->max_backoff > 3600) {
406 s->max_backoff = 3600;
412 s->rate_limit = atoi(optarg);
413 if (s->rate_limit < 1) {
414 ovs_fatal(0, "--rate-limit argument must be at least 1");
417 s->rate_limit = 1000;
421 case OPT_BURST_LIMIT:
422 s->burst_limit = atoi(optarg);
423 if (s->burst_limit < 1) {
424 ovs_fatal(0, "--burst-limit argument must be at least 1");
429 s->enable_stp = true;
433 s->enable_stp = false;
436 case OPT_OUT_OF_BAND:
445 svec_add(&s->netflow, optarg);
449 if (strlen(optarg) != 12
450 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
451 ovs_fatal(0, "argument to --mgmt-id must be "
452 "exactly 12 hex digits");
454 s->mgmt_id = strtoll(optarg, NULL, 16);
456 ovs_fatal(0, "argument to --mgmt-id must be nonzero");
461 svec_add(&s->listeners, optarg);
465 svec_add(&s->snoops, optarg);
469 svec_split(&s->ports, optarg, ",");
476 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
479 DAEMON_OPTION_HANDLERS
483 LEAK_CHECKER_OPTION_HANDLERS
486 STREAM_SSL_OPTION_HANDLERS
488 case OPT_BOOTSTRAP_CA_CERT:
489 stream_ssl_set_ca_cert_file(optarg, true);
504 if (argc < 1 || argc > 2) {
505 ovs_fatal(0, "need one or two non-option arguments; "
506 "use --help for usage");
509 /* Local and remote vconns. */
510 dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
512 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
514 /* Set accept_controller_regex. */
515 if (!s->accept_controller_re) {
516 s->accept_controller_re
517 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
520 /* Mode of operation. */
521 s->discovery = s->controller_name == NULL;
522 if (s->discovery && !s->in_band) {
523 ovs_fatal(0, "Cannot perform discovery with out-of-band control");
527 if (s->rate_limit && s->rate_limit < 100) {
528 VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
535 printf("%s: an OpenFlow switch implementation.\n"
536 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
537 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
538 "CONTROLLER is an active OpenFlow connection method; if it is\n"
539 "omitted, then ovs-openflowd performs controller discovery.\n",
540 program_name, program_name);
541 vconn_usage(true, true, true);
542 printf("\nOpenFlow options:\n"
543 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
544 " (ID must consist of 12 hex digits)\n"
545 " --mgmt-id=ID Use ID as the management ID\n"
546 " (ID must consist of 12 hex digits)\n"
547 " --manufacturer=MFR Identify manufacturer as MFR\n"
548 " --hardware=HW Identify hardware as HW\n"
549 " --software=SW Identify software as SW\n"
550 " --serial=SERIAL Identify serial number as SERIAL\n"
551 "\nController discovery options:\n"
552 " --accept-vconn=REGEX accept matching discovered controllers\n"
553 " --no-resolv-conf do not update /etc/resolv.conf\n"
554 "\nNetworking options:\n"
555 " --fail=open|closed when controller connection fails:\n"
556 " closed: drop all packets\n"
557 " open (default): act as learning switch\n"
558 " --inactivity-probe=SECS time between inactivity probes\n"
559 " --max-idle=SECS max idle for flows set up by switch\n"
560 " --max-backoff=SECS max time between controller connection\n"
561 " attempts (default: 8 seconds)\n"
562 " -l, --listen=METHOD allow management connections on METHOD\n"
563 " (a passive OpenFlow connection method)\n"
564 " --snoop=METHOD allow controller snooping on METHOD\n"
565 " (a passive OpenFlow connection method)\n"
566 " --out-of-band controller connection is out-of-band\n"
567 " --netflow=HOST:PORT configure NetFlow output target\n"
568 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
569 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
570 " --burst-limit=BURST limit on packet credit for idle time\n");
573 printf("\nOther options:\n"
574 " -h, --help display this help message\n"
575 " -V, --version display version information\n");
576 leak_checker_usage();