Add support for multiple OpenFlow controllers on a single bridge.
[sliver-openvswitch.git] / utilities / ovs-openflowd.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <assert.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <string.h>
26
27 #include "command-line.h"
28 #include "compiler.h"
29 #include "daemon.h"
30 #include "dirs.h"
31 #include "dpif.h"
32 #include "leak-checker.h"
33 #include "list.h"
34 #include "netdev.h"
35 #include "ofpbuf.h"
36 #include "ofproto/ofproto.h"
37 #include "openflow/openflow.h"
38 #include "packets.h"
39 #include "poll-loop.h"
40 #include "rconn.h"
41 #include "stream-ssl.h"
42 #include "svec.h"
43 #include "timeval.h"
44 #include "unixctl.h"
45 #include "util.h"
46 #include "vconn.h"
47
48 #include "vlog.h"
49 #define THIS_MODULE VLM_openflowd
50
51 /* Settings that may be configured by the user. */
52 struct ofsettings {
53     /* Controller configuration. */
54     struct ofproto_controller *controllers;
55     size_t n_controllers;
56
57     /* Datapath. */
58     uint64_t datapath_id;       /* Datapath ID. */
59     char *dp_name;              /* Name of local datapath. */
60     char *dp_type;              /* Type of local datapath. */
61     struct svec ports;          /* Set of ports to add to datapath (if any). */
62
63     /* Description strings. */
64     const char *mfr_desc;       /* Manufacturer. */
65     const char *hw_desc;        /* Hardware. */
66     const char *sw_desc;        /* Software version. */
67     const char *serial_desc;    /* Serial number. */
68     const char *dp_desc;        /* Datapath description. */
69
70     /* Related vconns and network devices. */
71     struct svec listeners;       /* Listen for management connections. */
72     struct svec snoops;          /* Listen for controller snooping conns. */
73
74     /* Failure behavior. */
75     int max_idle;             /* Idle time for flows in fail-open mode. */
76
77     /* Spanning tree protocol. */
78     bool enable_stp;
79
80     /* NetFlow. */
81     struct svec netflow;        /* NetFlow targets. */
82 };
83
84 static void parse_options(int argc, char *argv[], struct ofsettings *);
85 static void usage(void) NO_RETURN;
86
87 int
88 main(int argc, char *argv[])
89 {
90     struct unixctl_server *unixctl;
91     struct ofproto *ofproto;
92     struct ofsettings s;
93     int error;
94     struct dpif *dpif;
95     struct netflow_options nf_options;
96
97     proctitle_init(argc, argv);
98     set_program_name(argv[0]);
99     time_init();
100     vlog_init();
101     parse_options(argc, argv, &s);
102     signal(SIGPIPE, SIG_IGN);
103
104     die_if_already_running();
105     daemonize_start();
106
107     /* Start listening for ovs-appctl requests. */
108     error = unixctl_server_create(NULL, &unixctl);
109     if (error) {
110         exit(EXIT_FAILURE);
111     }
112
113     VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
114     VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
115
116     error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
117     if (error) {
118         ovs_fatal(error, "could not create datapath");
119     }
120
121     /* Add ports to the datapath if requested by the user. */
122     if (s.ports.n) {
123         const char *port;
124         size_t i;
125         struct netdev *netdev;
126
127         SVEC_FOR_EACH (i, port, &s.ports) {
128             error = netdev_open_default(port, &netdev);
129             if (error) {
130                 ovs_fatal(error, "failed to open %s as a device", port);
131             }
132
133             error = dpif_port_add(dpif, port, 0, NULL);
134             if (error) {
135                 ovs_fatal(error, "failed to add %s as a port", port);
136             }
137         }
138     }
139
140     /* Start OpenFlow processing. */
141     error = ofproto_create(s.dp_name, s.dp_type, NULL, NULL, &ofproto);
142     if (error) {
143         ovs_fatal(error, "could not initialize openflow switch");
144     }
145     if (s.datapath_id) {
146         ofproto_set_datapath_id(ofproto, s.datapath_id);
147     }
148     ofproto_set_desc(ofproto, s.mfr_desc, s.hw_desc, s.sw_desc,
149                      s.serial_desc, s.dp_desc);
150     if (!s.listeners.n) {
151         svec_add_nocopy(&s.listeners, xasprintf("punix:%s/%s.mgmt",
152                                               ovs_rundir, s.dp_name));
153     } else if (s.listeners.n == 1 && !strcmp(s.listeners.names[0], "none")) {
154         svec_clear(&s.listeners);
155     }
156     error = ofproto_set_listeners(ofproto, &s.listeners);
157     if (error) {
158         ovs_fatal(error, "failed to configure management connections");
159     }
160     error = ofproto_set_snoops(ofproto, &s.snoops);
161     if (error) {
162         ovs_fatal(error,
163                   "failed to configure controller snooping connections");
164     }
165     memset(&nf_options, 0, sizeof nf_options);
166     nf_options.collectors = s.netflow;
167     error = ofproto_set_netflow(ofproto, &nf_options);
168     if (error) {
169         ovs_fatal(error, "failed to configure NetFlow collectors");
170     }
171     error = ofproto_set_stp(ofproto, s.enable_stp);
172     if (error) {
173         ovs_fatal(error, "failed to configure STP");
174     }
175     ofproto_set_controllers(ofproto, s.controllers, s.n_controllers);
176
177     daemonize_complete();
178
179     while (ofproto_is_alive(ofproto)) {
180         error = ofproto_run(ofproto);
181         if (error) {
182             ovs_fatal(error, "unrecoverable datapath error");
183         }
184         unixctl_server_run(unixctl);
185         dp_run();
186         netdev_run();
187
188         ofproto_wait(ofproto);
189         unixctl_server_wait(unixctl);
190         dp_wait();
191         netdev_wait();
192         poll_block();
193     }
194
195     dpif_close(dpif);
196
197     return 0;
198 }
199 \f
200 /* User interface. */
201
202 static void
203 parse_options(int argc, char *argv[], struct ofsettings *s)
204 {
205     enum {
206         OPT_DATAPATH_ID = UCHAR_MAX + 1,
207         OPT_MFR_DESC,
208         OPT_HW_DESC,
209         OPT_SW_DESC,
210         OPT_SERIAL_DESC,
211         OPT_DP_DESC,
212         OPT_ACCEPT_VCONN,
213         OPT_NO_RESOLV_CONF,
214         OPT_BR_NAME,
215         OPT_FAIL_MODE,
216         OPT_INACTIVITY_PROBE,
217         OPT_MAX_IDLE,
218         OPT_MAX_BACKOFF,
219         OPT_SNOOP,
220         OPT_RATE_LIMIT,
221         OPT_BURST_LIMIT,
222         OPT_BOOTSTRAP_CA_CERT,
223         OPT_STP,
224         OPT_NO_STP,
225         OPT_OUT_OF_BAND,
226         OPT_IN_BAND,
227         OPT_NETFLOW,
228         OPT_PORTS,
229         VLOG_OPTION_ENUMS,
230         LEAK_CHECKER_OPTION_ENUMS
231     };
232     static struct option long_options[] = {
233         {"datapath-id", required_argument, 0, OPT_DATAPATH_ID},
234         {"mfr-desc", required_argument, 0, OPT_MFR_DESC},
235         {"hw-desc", required_argument, 0, OPT_HW_DESC},
236         {"sw-desc", required_argument, 0, OPT_SW_DESC},
237         {"serial-desc", required_argument, 0, OPT_SERIAL_DESC},
238         {"dp-desc", required_argument, 0, OPT_DP_DESC},
239         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
240         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
241         {"config",      required_argument, 0, 'F'},
242         {"br-name",     required_argument, 0, OPT_BR_NAME},
243         {"fail",        required_argument, 0, OPT_FAIL_MODE},
244         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
245         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
246         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
247         {"listen",      required_argument, 0, 'l'},
248         {"snoop",      required_argument, 0, OPT_SNOOP},
249         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
250         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
251         {"stp",         no_argument, 0, OPT_STP},
252         {"no-stp",      no_argument, 0, OPT_NO_STP},
253         {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
254         {"in-band",     no_argument, 0, OPT_IN_BAND},
255         {"netflow",     required_argument, 0, OPT_NETFLOW},
256         {"ports",       required_argument, 0, OPT_PORTS},
257         {"verbose",     optional_argument, 0, 'v'},
258         {"help",        no_argument, 0, 'h'},
259         {"version",     no_argument, 0, 'V'},
260         DAEMON_LONG_OPTIONS,
261         VLOG_LONG_OPTIONS,
262         LEAK_CHECKER_LONG_OPTIONS,
263 #ifdef HAVE_OPENSSL
264         STREAM_SSL_LONG_OPTIONS
265         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
266 #endif
267         {0, 0, 0, 0},
268     };
269     char *short_options = long_options_to_short_options(long_options);
270     struct ofproto_controller controller_opts;
271
272     /* Set defaults that we can figure out before parsing options. */
273     controller_opts.target = NULL;
274     controller_opts.max_backoff = 8;
275     controller_opts.probe_interval = 5;
276     controller_opts.fail = OFPROTO_FAIL_STANDALONE;
277     controller_opts.band = OFPROTO_IN_BAND;
278     controller_opts.accept_re = NULL;
279     controller_opts.update_resolv_conf = true;
280     controller_opts.rate_limit = 0;
281     controller_opts.burst_limit = 0;
282     s->datapath_id = 0;
283     s->mfr_desc = NULL;
284     s->hw_desc = NULL;
285     s->sw_desc = NULL;
286     s->serial_desc = NULL;
287     s->dp_desc = NULL;
288     svec_init(&s->listeners);
289     svec_init(&s->snoops);
290     s->max_idle = 0;
291     s->enable_stp = false;
292     svec_init(&s->netflow);
293     svec_init(&s->ports);
294     for (;;) {
295         int c;
296
297         c = getopt_long(argc, argv, short_options, long_options, NULL);
298         if (c == -1) {
299             break;
300         }
301
302         switch (c) {
303         case OPT_DATAPATH_ID:
304             if (!dpid_from_string(optarg, &s->datapath_id)) {
305                 ovs_fatal(0, "argument to --datapath-id must be "
306                           "exactly 16 hex digits and may not be all-zero");
307             }
308             break;
309
310         case OPT_MFR_DESC:
311             s->mfr_desc = optarg;
312             break;
313
314         case OPT_HW_DESC:
315             s->hw_desc = optarg;
316             break;
317
318         case OPT_SW_DESC:
319             s->sw_desc = optarg;
320             break;
321
322         case OPT_SERIAL_DESC:
323             s->serial_desc = optarg;
324             break;
325
326         case OPT_DP_DESC:
327             s->dp_desc = optarg;
328             break;
329
330         case OPT_ACCEPT_VCONN:
331             controller_opts.accept_re = optarg;
332             break;
333
334         case OPT_NO_RESOLV_CONF:
335             controller_opts.update_resolv_conf = false;
336             break;
337
338         case OPT_FAIL_MODE:
339             if (!strcmp(optarg, "open")) {
340                 controller_opts.fail = OFPROTO_FAIL_STANDALONE;
341             } else if (!strcmp(optarg, "closed")) {
342                 controller_opts.fail = OFPROTO_FAIL_SECURE;
343             } else {
344                 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
345             }
346             break;
347
348         case OPT_INACTIVITY_PROBE:
349             controller_opts.probe_interval = atoi(optarg);
350             if (controller_opts.probe_interval < 5) {
351                 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
352             }
353             break;
354
355         case OPT_MAX_IDLE:
356             if (!strcmp(optarg, "permanent")) {
357                 s->max_idle = OFP_FLOW_PERMANENT;
358             } else {
359                 s->max_idle = atoi(optarg);
360                 if (s->max_idle < 1 || s->max_idle > 65535) {
361                     ovs_fatal(0, "--max-idle argument must be between 1 and "
362                               "65535 or the word 'permanent'");
363                 }
364             }
365             break;
366
367         case OPT_MAX_BACKOFF:
368             controller_opts.max_backoff = atoi(optarg);
369             if (controller_opts.max_backoff < 1) {
370                 ovs_fatal(0, "--max-backoff argument must be at least 1");
371             } else if (controller_opts.max_backoff > 3600) {
372                 controller_opts.max_backoff = 3600;
373             }
374             break;
375
376         case OPT_RATE_LIMIT:
377             if (optarg) {
378                 controller_opts.rate_limit = atoi(optarg);
379                 if (controller_opts.rate_limit < 1) {
380                     ovs_fatal(0, "--rate-limit argument must be at least 1");
381                 }
382             } else {
383                 controller_opts.rate_limit = 1000;
384             }
385             break;
386
387         case OPT_BURST_LIMIT:
388             controller_opts.burst_limit = atoi(optarg);
389             if (controller_opts.burst_limit < 1) {
390                 ovs_fatal(0, "--burst-limit argument must be at least 1");
391             }
392             break;
393
394         case OPT_STP:
395             s->enable_stp = true;
396             break;
397
398         case OPT_NO_STP:
399             s->enable_stp = false;
400             break;
401
402         case OPT_OUT_OF_BAND:
403             controller_opts.band = OFPROTO_OUT_OF_BAND;
404             break;
405
406         case OPT_IN_BAND:
407             controller_opts.band = OFPROTO_IN_BAND;
408             break;
409
410         case OPT_NETFLOW:
411             svec_add(&s->netflow, optarg);
412             break;
413
414         case 'l':
415             svec_add(&s->listeners, optarg);
416             break;
417
418         case OPT_SNOOP:
419             svec_add(&s->snoops, optarg);
420             break;
421
422         case OPT_PORTS:
423             svec_split(&s->ports, optarg, ",");
424             break;
425
426         case 'h':
427             usage();
428
429         case 'V':
430             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
431             exit(EXIT_SUCCESS);
432
433         DAEMON_OPTION_HANDLERS
434
435         VLOG_OPTION_HANDLERS
436
437         LEAK_CHECKER_OPTION_HANDLERS
438
439 #ifdef HAVE_OPENSSL
440         STREAM_SSL_OPTION_HANDLERS
441
442         case OPT_BOOTSTRAP_CA_CERT:
443             stream_ssl_set_ca_cert_file(optarg, true);
444             break;
445 #endif
446
447         case '?':
448             exit(EXIT_FAILURE);
449
450         default:
451             abort();
452         }
453     }
454     free(short_options);
455
456     argc -= optind;
457     argv += optind;
458     if (argc < 1 || argc > 2) {
459         ovs_fatal(0, "need one or two non-option arguments; "
460                   "use --help for usage");
461     }
462
463     /* Set accept_controller_regex. */
464     if (!controller_opts.accept_re) {
465         controller_opts.accept_re
466             = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
467     }
468
469     /* Rate limiting. */
470     if (controller_opts.rate_limit && controller_opts.rate_limit < 100) {
471         VLOG_WARN("Rate limit set to unusually low value %d",
472                   controller_opts.rate_limit);
473     }
474
475     /* Local vconns. */
476     dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
477
478     /* Controllers. */
479     s->n_controllers = argc > 1 ? argc - 1 : 1;
480     s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
481     if (argc > 1) {
482         size_t i;
483
484         for (i = 0; i < s->n_controllers; i++) {
485             s->controllers[i] = controller_opts;
486             s->controllers[i].target = argv[i + 1];
487         }
488     } else {
489         s->controllers[0] = controller_opts;
490         s->controllers[0].target = "discover";
491     }
492
493     /* Sanity check. */
494     if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
495         size_t i;
496
497         for (i = 0; i < s->n_controllers; i++) {
498             if (!strcmp(s->controllers[i].target, "discover")) {
499                 ovs_fatal(0, "Cannot perform discovery with out-of-band "
500                           "control");
501             }
502         }
503     }
504 }
505
506 static void
507 usage(void)
508 {
509     printf("%s: an OpenFlow switch implementation.\n"
510            "usage: %s [OPTIONS] DATAPATH [CONTROLLER...]\n"
511            "DATAPATH is a local datapath (e.g. \"dp0\").\n"
512            "Each CONTROLLER is an active OpenFlow connection method.  If\n"
513            "none is given, ovs-openflowd performs controller discovery.\n",
514            program_name, program_name);
515     vconn_usage(true, true, true);
516     printf("\nOpenFlow options:\n"
517            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
518            "                          (ID must consist of 16 hex digits)\n"
519            "  --mfr-desc=MFR          Identify manufacturer as MFR\n"
520            "  --hw-desc=HW            Identify hardware as HW\n"
521            "  --sw-desc=SW            Identify software as SW\n"
522            "  --serial-desc=SERIAL    Identify serial number as SERIAL\n"
523            "  --dp-desc=DP_DESC       Identify dp description as DP_DESC\n"
524            "\nController discovery options:\n"
525            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
526            "  --no-resolv-conf        do not update /etc/resolv.conf\n"
527            "\nNetworking options:\n"
528            "  --fail=open|closed      when controller connection fails:\n"
529            "                            closed: drop all packets\n"
530            "                            open (default): act as learning switch\n"
531            "  --inactivity-probe=SECS time between inactivity probes\n"
532            "  --max-idle=SECS         max idle for flows set up by switch\n"
533            "  --max-backoff=SECS      max time between controller connection\n"
534            "                          attempts (default: 8 seconds)\n"
535            "  -l, --listen=METHOD     allow management connections on METHOD\n"
536            "                          (a passive OpenFlow connection method)\n"
537            "  --snoop=METHOD          allow controller snooping on METHOD\n"
538            "                          (a passive OpenFlow connection method)\n"
539            "  --out-of-band           controller connection is out-of-band\n"
540            "  --netflow=HOST:PORT     configure NetFlow output target\n"
541            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
542            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
543            "  --burst-limit=BURST     limit on packet credit for idle time\n");
544     daemon_usage();
545     vlog_usage();
546     printf("\nOther options:\n"
547            "  -h, --help              display this help message\n"
548            "  -V, --version           display version information\n");
549     leak_checker_usage();
550     exit(EXIT_SUCCESS);
551 }