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"
31 #include "leak-checker.h"
35 #include "ofproto/ofproto.h"
36 #include "ofproto/wdp.h"
37 #include "openflow/openflow.h"
39 #include "poll-loop.h"
41 #include "stream-ssl.h"
50 #define THIS_MODULE VLM_openflowd
52 /* Settings that may be configured by the user. */
54 /* Controller configuration. */
55 struct ofproto_controller *controllers;
59 uint64_t datapath_id; /* Datapath ID. */
60 char *dp_name; /* Name of local datapath. */
61 char *dp_type; /* Type of local datapath. */
62 struct svec ports; /* Set of ports to add to datapath (if any). */
64 /* Description strings. */
65 const char *mfr_desc; /* Manufacturer. */
66 const char *hw_desc; /* Hardware. */
67 const char *sw_desc; /* Software version. */
68 const char *serial_desc; /* Serial number. */
69 const char *dp_desc; /* Datapath description. */
71 /* Related vconns and network devices. */
72 struct svec listeners; /* Listen for management connections. */
73 struct svec snoops; /* Listen for controller snooping conns. */
75 /* Failure behavior. */
76 int max_idle; /* Idle time for flows in fail-open mode. */
78 /* Spanning tree protocol. */
82 struct svec netflow; /* NetFlow targets. */
85 static void parse_options(int argc, char *argv[], struct ofsettings *);
86 static void usage(void) NO_RETURN;
89 main(int argc, char *argv[])
91 struct unixctl_server *unixctl;
92 struct ofproto *ofproto;
96 struct netflow_options nf_options;
98 proctitle_init(argc, argv);
99 set_program_name(argv[0]);
102 parse_options(argc, argv, &s);
103 signal(SIGPIPE, SIG_IGN);
105 die_if_already_running();
108 /* Start listening for ovs-appctl requests. */
109 error = unixctl_server_create(NULL, &unixctl);
114 VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
115 VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
117 error = xfif_create_and_open(s.dp_name, s.dp_type, &xfif);
119 ovs_fatal(error, "could not create datapath");
122 /* Add ports to the datapath if requested by the user. */
126 struct netdev *netdev;
128 SVEC_FOR_EACH (i, port, &s.ports) {
129 error = netdev_open_default(port, &netdev);
131 ovs_fatal(error, "failed to open %s as a device", port);
134 error = xfif_port_add(xfif, port, 0, NULL);
136 ovs_fatal(error, "failed to add %s as a port", port);
141 /* Start OpenFlow processing. */
142 error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
144 ovs_fatal(error, "could not initialize openflow switch");
147 ofproto_set_datapath_id(ofproto, s.datapath_id);
149 ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
150 s.serial_desc, s.dp_desc);
151 if (!s.listeners.n) {
152 svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
153 ovs_rundir, s.dp_name));
154 } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
155 svec_clear(&s.listeners);
157 error = ofproto_set_listeners(ofproto, &s.listeners);
159 ovs_fatal(error, "failed to configure management connections");
161 error = ofproto_set_snoops(ofproto, &s.snoops);
164 "failed to configure controller snooping connections");
166 memset(&nf_options, 0, sizeof nf_options);
167 nf_options.collectors = s.netflow;
168 error = ofproto_set_netflow(ofproto, &nf_options);
170 ovs_fatal(error, "failed to configure NetFlow collectors");
172 error = ofproto_set_stp(ofproto, s.enable_stp);
174 ovs_fatal(error, "failed to configure STP");
176 ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
178 daemonize_complete();
180 while (ofproto_is_alive(ofproto)) {
181 error = ofproto_run(ofproto);
183 ovs_fatal(error, "unrecoverable datapath error");
185 unixctl_server_run(unixctl);
189 ofproto_wait(ofproto);
190 unixctl_server_wait(unixctl);
201 /* User interface. */
204 parse_options(int argc, char *argv[], struct ofsettings *s)
207 OPT_DATAPATH_ID = UCHAR_MAX + 1,
217 OPT_INACTIVITY_PROBE,
223 OPT_BOOTSTRAP_CA_CERT,
231 LEAK_CHECKER_OPTION_ENUMS
233 static struct option long_options[] = {
234 {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
235 {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
236 {"hw-desc", required_argument, 0, OPT_HW_DESC},
237 {"sw-desc", required_argument, 0, OPT_SW_DESC},
238 {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
239 {"dp-desc", required_argument, 0, OPT_DP_DESC},
240 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
241 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
242 {"config", required_argument, 0, 'F'},
243 {"br-name", required_argument, 0, OPT_BR_NAME},
244 {"fail", required_argument, 0, OPT_FAIL_MODE},
245 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
246 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
247 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
248 {"listen", required_argument, 0, 'l'},
249 {"snoop", required_argument, 0, OPT_SNOOP},
250 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
251 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
252 {"stp", no_argument, 0, OPT_STP},
253 {"no-stp", no_argument, 0, OPT_NO_STP},
254 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
255 {"in-band", no_argument, 0, OPT_IN_BAND},
256 {"netflow", required_argument, 0, OPT_NETFLOW},
257 {"ports", required_argument, 0, OPT_PORTS},
258 {"verbose", optional_argument, 0, 'v'},
259 {"help", no_argument, 0, 'h'},
260 {"version", no_argument, 0, 'V'},
263 LEAK_CHECKER_LONG_OPTIONS,
265 STREAM_SSL_LONG_OPTIONS
266 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
270 char *short_options = long_options_to_short_options(long_options);
271 struct ofproto_controller controller_opts;
273 /* Set defaults that we can figure out before parsing options. */
274 controller_opts.target = NULL;
275 controller_opts.max_backoff = 8;
276 controller_opts.probe_interval = 5;
277 controller_opts.fail = OFPROTO_FAIL_STANDALONE;
278 controller_opts.band = OFPROTO_IN_BAND;
279 controller_opts.accept_re = NULL;
280 controller_opts.update_resolv_conf = true;
281 controller_opts.rate_limit = 0;
282 controller_opts.burst_limit = 0;
287 s->serial_desc = NULL;
289 svec_init(&s->listeners);
290 svec_init(&s->snoops);
292 s->enable_stp = false;
293 svec_init(&s->netflow);
294 svec_init(&s->ports);
298 c = getopt_long(argc, argv, short_options, long_options, NULL);
304 case OPT_DATAPATH_ID:
305 if (!dpid_from_string(optarg, &s->datapath_id)) {
306 ovs_fatal(0, "argument to --datapath-id must be "
307 "exactly 16 hex digits and may not be all-zero");
312 s->mfr_desc = optarg;
323 case OPT_SERIAL_DESC:
324 s->serial_desc = optarg;
331 case OPT_ACCEPT_VCONN:
332 controller_opts.accept_re = optarg;
335 case OPT_NO_RESOLV_CONF:
336 controller_opts.update_resolv_conf = false;
340 if (!strcmp(optarg, "open") || !strcmp(optarg, "standalone")) {
341 controller_opts.fail = OFPROTO_FAIL_STANDALONE;
342 } else if (!strcmp(optarg, "closed")
343 || !strcmp(optarg, "secure")) {
344 controller_opts.fail = OFPROTO_FAIL_SECURE;
346 ovs_fatal(0, "--fail argument must be \"standalone\" "
351 case OPT_INACTIVITY_PROBE:
352 controller_opts.probe_interval = atoi(optarg);
353 if (controller_opts.probe_interval < 5) {
354 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
359 if (!strcmp(optarg, "permanent")) {
360 s->max_idle = OFP_FLOW_PERMANENT;
362 s->max_idle = atoi(optarg);
363 if (s->max_idle < 1 || s->max_idle > 65535) {
364 ovs_fatal(0, "--max-idle argument must be between 1 and "
365 "65535 or the word 'permanent'");
370 case OPT_MAX_BACKOFF:
371 controller_opts.max_backoff = atoi(optarg);
372 if (controller_opts.max_backoff < 1) {
373 ovs_fatal(0, "--max-backoff argument must be at least 1");
374 } else if (controller_opts.max_backoff > 3600) {
375 controller_opts.max_backoff = 3600;
381 controller_opts.rate_limit = atoi(optarg);
382 if (controller_opts.rate_limit < 1) {
383 ovs_fatal(0, "--rate-limit argument must be at least 1");
386 controller_opts.rate_limit = 1000;
390 case OPT_BURST_LIMIT:
391 controller_opts.burst_limit = atoi(optarg);
392 if (controller_opts.burst_limit < 1) {
393 ovs_fatal(0, "--burst-limit argument must be at least 1");
398 s->enable_stp = true;
402 s->enable_stp = false;
405 case OPT_OUT_OF_BAND:
406 controller_opts.band = OFPROTO_OUT_OF_BAND;
410 controller_opts.band = OFPROTO_IN_BAND;
414 svec_add(&s->netflow, optarg);
418 svec_add(&s->listeners, optarg);
422 svec_add(&s->snoops, optarg);
426 svec_split(&s->ports, optarg, ",");
433 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
436 DAEMON_OPTION_HANDLERS
440 LEAK_CHECKER_OPTION_HANDLERS
443 STREAM_SSL_OPTION_HANDLERS
445 case OPT_BOOTSTRAP_CA_CERT:
446 stream_ssl_set_ca_cert_file(optarg, true);
461 if (argc < 1 || argc > 2) {
462 ovs_fatal(0, "need one or two non-option arguments; "
463 "use --help for usage");
466 /* Set accept_controller_regex. */
467 if (!controller_opts.accept_re) {
468 controller_opts.accept_re
469 = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
473 if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
474 VLOG_WARN("Rate limit set to unusually low value %d",
475 controller_opts.rate_limit);
479 xf_parse_name(argv[0], &s->dp_name, &s->dp_type);
482 s->n_controllers = argc > 1 ? argc - 1 : 1;
483 s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
487 for (i = 0; i < s->n_controllers; i++) {
488 s->controllers[i] = controller_opts;
489 s->controllers[i].target = argv[i + 1];
492 s->controllers[0] = controller_opts;
493 s->controllers[0].target = "discover";
497 if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
500 for (i = 0; i < s->n_controllers; i++) {
501 if (!strcmp(s->controllers[i].target, "discover")) {
502 ovs_fatal(0, "Cannot perform discovery with out-of-band "
512 printf("%s: an OpenFlow switch implementation.\n"
513 "usage: %s [OPTIONS] DATAPATH [CONTROLLER...]\n"
514 "DATAPATH is a local datapath (e.g. \"dp0\").\n"
515 "Each CONTROLLER is an active OpenFlow connection method. If\n"
516 "none is given, ovs-openflowd performs controller discovery.\n",
517 program_name, program_name);
518 vconn_usage(true, true, true);
519 printf("\nOpenFlow options:\n"
520 " -d, --datapath-id=ID Use ID as the OpenFlow switch ID\n"
521 " (ID must consist of 16 hex digits)\n"
522 " --mfr-desc=MFR Identify manufacturer as MFR\n"
523 " --hw-desc=HW Identify hardware as HW\n"
524 " --sw-desc=SW Identify software as SW\n"
525 " --serial-desc=SERIAL Identify serial number as SERIAL\n"
526 " --dp-desc=DP_DESC Identify dp description as DP_DESC\n"
527 "\nController discovery options:\n"
528 " --accept-vconn=REGEX accept matching discovered controllers\n"
529 " --no-resolv-conf do not update /etc/resolv.conf\n"
530 "\nNetworking options:\n"
531 " --fail=open|closed when controller connection fails:\n"
532 " closed: drop all packets\n"
533 " open (default): act as learning switch\n"
534 " --inactivity-probe=SECS time between inactivity probes\n"
535 " --max-idle=SECS max idle for flows set up by switch\n"
536 " --max-backoff=SECS max time between controller connection\n"
537 " attempts (default: 8 seconds)\n"
538 " -l, --listen=METHOD allow management connections on METHOD\n"
539 " (a passive OpenFlow connection method)\n"
540 " --snoop=METHOD allow controller snooping on METHOD\n"
541 " (a passive OpenFlow connection method)\n"
542 " --out-of-band controller connection is out-of-band\n"
543 " --netflow=HOST:PORT configure NetFlow output target\n"
544 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
545 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
546 " --burst-limit=BURST limit on packet credit for idle time\n");
549 printf("\nOther options:\n"
550 " -h, --help display this help message\n"
551 " -V, --version display version information\n");
552 leak_checker_usage();